Person类重载了equals方法,预期插入Person数组到pSet中应该是只有四个Person对象,结果却不是。
结果:[(Gregg,50), (Kay,30), (Bill,20), (Ann,30), (Gregg,25)]。。为什么呢?
package HashSet;
import java.util.Arrays;
import java.util.HashSet;class Person implements Comparable {
private String name;  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 int compareTo(Object p) {
return name.compareTo(((Person) p).name); }
@Override
public boolean equals(Object p) {
return name.equals(((Person)p).name);
}
@Override
public String toString() {
return "(" + name + "," + age + ")";
}
}public class MapHashSet { public static void main(String[] ar) {
HashSet hashset1 = new HashSet();
hashset1.add(new Integer(40));
hashset1.add(new Integer(60));
System.out.println(hashset1); // 说明hashset里面的元素是会自动排序的
hashset1.add(new Integer(50));
System.out.println(hashset1); // 说明hashset 里面的元素是不可以重复的
hashset1.add(new Integer(50));
System.out.println(hashset1); System.out.println(hashset1.contains(new Integer(50)));
System.out.println(hashset1.contains(new Integer(70))); HashSet hashset2 = new HashSet();
hashset2.add(new Integer(30));
hashset2.add(new Integer(40));
hashset2.add(new Integer(50));
System.out.println(hashset2); hashset1.addAll(hashset2);
System.out.println(hashset2); hashset1.removeAll(hashset2);
System.out.println(hashset1); hashset1.remove(new Integer(30));
System.out.println(hashset1); hashset1.retainAll(hashset2);
System.out.println(hashset1); hashset1.add(new Integer(60));
System.out.println(hashset1);
hashset1.removeAll(hashset2);
System.out.println(hashset1); hashset1.add(null);
System.out.println(hashset1); HashSet pSet = new HashSet();
Person[] p = { new Person("Gregg", 25), new Person("Ann", 30),
new Person("Bill", 20), new Person("Gregg",90),new Person("Kay", 30)};
for(int i=0;i<p.length;i++){
pSet.add(p[i]);
}
System.out.println(pSet); java.util.Iterator it= pSet.iterator();
((Person)it.next()).age=50; System.out.println(pSet); pSet.add(new Person("Craig",40));
System.out.println(pSet); for(int i=0;i<p.length;i++){
System.out.println(p[i]+" "+pSet.contains(p[i])); } Person[] pArray=(Person[])pSet.toArray(new Person[0]); for(int i=0;i<p.length;i++){
System.out.println(pArray[i]+" ");
System.out.println();
}
        Arrays.sort(pArray);
        for(int i=0;i<p.length;i++){
         System.out.println(pArray[i]+"");
        }
        System.out.println();
        System.out.println(pSet);
}}

解决方案 »

  1.   

    重写equals时hashCode方法也要重写
    集合中两个对象比较的顺序:先调用hashCode(),然后调用equals();
    hashCode相等时,继续调用equals,表示两个对象可能相等可能不相等;
    不相等时不调用equals,表示两个对象一定不相等;
      

  2.   

    重写equals时hashCode方法也要重写
    集合中两个对象比较的顺序:先调用hashCode(),然后调用equals();
    hashCode相等时,继续调用equals,表示两个对象可能相等可能不相等;
    不相等时不调用equals,表示两个对象一定不相等;
      

  3.   

                   public int hashCode() {

    int result = 17;
    result = 37 * result + this.name.hashCode();
    return result;
    }
    //把这个方法加进Person 去
      

  4.   

    hashset实际上是基于hashmap来实现的(内部有一个hashmap实例)。map的key是不允许重复的,而hashset在添加元素的时候,实际上是将要添加的元素当作key放进hashmap里面去,hashmap内部有一个哈希表用来存储数据,当map往哈希表里面放东西的时候,再用key值的hashcode去查有没有存在。是这样达到去重复的目的的。 所以楼主还要处理下hashcode方法。。
      

  5.   

    hashset 存储时KEY值是不能重复的,所以你先找为什么KEY值没有被判断出重复了.是不是要重写hashCode()方法.