HashSet为什么可以插入重复的项?

来源:互联网 发布:linux的copy命令 编辑:程序博客网 时间:2024/04/29 20:21
 
  1. import java.util.Arrays;
  2. import java.util.HashSet;
  3. class Person implements Comparable {
  4.     private String name;
  5.      int age;
  6.     private int hashcode = 0;
  7.     public Person(String n, int a) {
  8.         name = n;
  9.         age = a;
  10.         for (int i = 0; i < name.length(); i++) {
  11.             hashcode += name.charAt(i);
  12.         }
  13.     }
  14.     public int compareTo(Object p) {
  15.         return name.compareTo(((Person) p).name);
  16.     }
  17.     @Override
  18.     public boolean equals(Object p) {
  19.         return name.equals(((Person)p).name);
  20.     }
  21.     @Override
  22.     public String toString() {
  23.         return "(" + name + "," + age + ")";
  24.     }
  25. }
  26. public class MapHashSet {
  27.     public static void main(String[] ar) {
  28.         HashSet hashset1 = new HashSet();
  29.         hashset1.add(new Integer(40));
  30.         hashset1.add(new Integer(60));
  31.         System.out.println(hashset1);
  32.         // 说明hashset里面的元素是会自动排序的
  33.         hashset1.add(new Integer(50));
  34.         System.out.println(hashset1);
  35.         // 说明hashset 里面的元素是不可以重复的
  36.         hashset1.add(new Integer(50));
  37.         System.out.println(hashset1);
  38.         System.out.println(hashset1.contains(new Integer(50)));
  39.         System.out.println(hashset1.contains(new Integer(70)));
  40.         HashSet hashset2 = new HashSet();
  41.         hashset2.add(new Integer(30));
  42.         hashset2.add(new Integer(40));
  43.         hashset2.add(new Integer(50));
  44.         System.out.println(hashset2);
  45.         hashset1.addAll(hashset2);
  46.         System.out.println(hashset2);
  47.         hashset1.removeAll(hashset2);
  48.         System.out.println(hashset1);
  49.         hashset1.remove(new Integer(30));
  50.         System.out.println(hashset1);
  51.         hashset1.retainAll(hashset2);
  52.         System.out.println(hashset1);
  53.         hashset1.add(new Integer(60));
  54.         System.out.println(hashset1);
  55.         hashset1.removeAll(hashset2);
  56.         System.out.println(hashset1);
  57.         hashset1.add(null);
  58.         System.out.println(hashset1);
  59.         HashSet pSet = new HashSet();
  60.         Person[] p = { new Person("Gregg"25), new Person("Ann"30),
  61.                 new Person("Bill"20), new Person("Gregg",90),new Person("Kay"30)};
  62.         for(int i=0;i<p.length;i++){
  63.             pSet.add(p[i]);
  64.         }
  65.         System.out.println(pSet);
  66.         java.util.Iterator it= pSet.iterator();
  67.         ((Person)it.next()).age=50;
  68.         System.out.println(pSet);
  69.         pSet.add(new Person("Craig",40));
  70.         System.out.println(pSet);
  71.         for(int i=0;i<p.length;i++){
  72.             System.out.println(p[i]+" "+pSet.contains(p[i]));
  73.         }
  74.         Person[] pArray=(Person[])pSet.toArray(new Person[0]);
  75.         for(int i=0;i<p.length;i++){
  76.             System.out.println(pArray[i]+" ");
  77.             System.out.println();
  78.         }
  79.         Arrays.sort(pArray);
  80.         for(int i=0;i<p.length;i++){
  81.             System.out.println(pArray[i]+"");
  82.         }
  83.         System.out.println();
  84.         System.out.println(pSet);
  85.     }
  86. }
原创粉丝点击