⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig10-19.java

📁 数据结构与算法Java语言版(美)Adam Drozdek著
💻 JAVA
字号:
import java.io.*;
import java.util.*;

class Person implements Comparable {
    private String name;
    public int age;
    private int hashcode = 0;
    public Person(String n, int a) {
        name = n; age = a;
        for (int i = 0; i < name.length(); i++)
            hashcode += name.charAt(i);
    }
    public Person() {
        this("",0);
    }
    public boolean equals(Object p) {
        return name.equals(((Person)p).name);
    }
    public int compareTo(Object p) {
        return name.compareTo(((Person)p).name);
    }
    public int hashCode() {
        return hashcode;
    }
    public String toString() {
        return "(" + name + "," + age  + ")";
    }
}

class TestHashSet {
    public static void main(String[] ar) {
        HashSet hashset1 = new HashSet();
        hashset1.add(new Integer(40));
        hashset1.add(new Integer(60));
        System.out.println(hashset1);      // [40, 60]
        hashset1.add(new Integer(50));
        System.out.println(hashset1);      // [40, 50, 60]
        hashset1.add(new Integer(50));
        System.out.println(hashset1);      // [40, 50, 60]
        System.out.println(hashset1.contains(new Integer(50)));  // true
        System.out.println(hashset1.contains(new Integer(70)));  // false
        HashSet hashset2 = new HashSet();
        hashset2.add(new Integer(30));
        hashset2.add(new Integer(40));
        hashset2.add(new Integer(50));     
        System.out.println(hashset2);      // [30, 40, 50]
        hashset1.addAll(hashset2);
        // union: [40, 50, 60] and [30, 40, 50] ==> [30, 40, 50, 60] 
        System.out.println(hashset1);      // [30, 40, 50, 60]
        hashset1.remove(new Integer(30));      
        System.out.println(hashset1);      // [40, 50, 60]
        hashset1.retainAll(hashset2);      // [40, 50]
        // intersection: [40, 50, 60] and [30, 40, 50] ==> [40, 50] 
        System.out.println(hashset1);      // [40, 50]
        hashset1.add(new Integer(60));     // [40, 50, 60]
        hashset1.removeAll(hashset2);
        // difference: [40, 50, 60] and [30, 40, 50] ==> [60] 
        System.out.println(hashset1);      // [60]
        hashset1.add(null);      
        System.out.println(hashset1);      // [60, null]

        HashSet pSet = new HashSet();
        Person[] p = {new Person("Gregg",25), new Person("Ann",30),
                      new Person("Bill",20),  new Person("Gregg",35),
                      new Person("Kay",30)};
        for (int i = 0; i < p.length; i++)
             pSet.add(p[i]);
        System.out.println(pSet);
        // [(Ann,30), (Gregg,25), (Kay,30), (Bill,20)]
        java.util.Iterator it = pSet.iterator();
        ((Person)it.next()).age = 50;
        System.out.println(pSet);
        // [(Ann,50), (Gregg,25), (Kay,30), (Bill,20)]
        pSet.add(new Person("Craig",40));
        System.out.println(pSet);
        // [(Ann,50), (Gregg,25), (Kay,30), (Craig,40), (Bill,20)]
        for (int i = 0; i < p.length; i++)
            System.out.println(p[i] + " " + pSet.contains(p[i]));
        // (Gregg,25) true
        // (Ann,50) true
        // (Bill,20) true
        // (Gregg,35) true
        // (Kay,30) true
        // Using an array to sort elements in the hashset:
        Person[] pArray = (Person[]) pSet.toArray(new Person[0]);
        for (int i = 0; i < p.length; i++)
            System.out.print(pArray[i] + " ");
        System.out.println();
        // (Ann,50) (Gregg,25) (Kay,30) (Craig,40) (Bill,20) 
        Arrays.sort(pArray);
        for (int i = 0; i < p.length; i++)
            System.out.print(pArray[i] + " ");
        System.out.println();
        // (Ann,50) (Bill,20) (Craig,40) (Gregg,25) (Kay,30) 
        System.out.println(pSet);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -