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

 
  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. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值