HashSet的存储问题

[code="java"]
public class Stu implements Comparable {
private String name;
private String address;
private String sex;
private int age;

public Stu(String name, String address, String sex, int age) {
    super();
    this.name = name;
    this.address = address;
    this.sex = sex;
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int compareTo(Stu o) {
    if (this.name.compareTo(o.name) > 0) {
        return 1;
    } else if (this.name.compareTo(o.name) < 0) {
        return -1;
    } else {
        return 0;
    }
}

public String toString() {
    return new StringBuilder().append("name = ").append(this.name).append(
            " address = ").append(this.address).append(" sex = ").append(
            this.sex).append(" age = ").append(this.age).toString();
}

public boolean equals(Stu obj) {
    return (this.name.equals(obj.name));
}

public int hashCode() {
    return this.name.hashCode();
}

}
[/code]

[code="java"]
public class CollectionTest {

private static Set<Stu> set = new HashSet<Stu>();

public static void main(String[] args) {

    initHashSet();
    System.out.println();
    printHashSet();
}


public static void initHashSet() {

    Stu stu1 = new Stu("stu9", "neu1", "male", 20);
    Stu stu2 = new Stu("stu2", "neu2", "female", 22);
    Stu stu3 = new Stu("stu6", "neu3", "male", 23);
    Stu stu4 = new Stu("stu4", "neu5", "male", 24);
    Stu stu5 = new Stu("stu4", "neu4", "female", 9);

    System.out.println("stu4.equals(stu5) = " + stu4.equals(stu5));
    System.out.println("stu4.hash = " + stu4.hashCode()+ " ; stu5.hash = " + stu5.hashCode());

    set.add(null);
    set.add(stu1);
    set.add(stu2);
    set.add(stu3);
    set.add(stu4);
    set.add(stu5);
}

public static void printHashSet() {
    for (Stu i : set) {
        System.out.println(i);
    }
}

}
[/code]

输出结果为:
[quote]stu4.equals(stu5) = true
stu4.hash = 3541120 ; stu5.hash = 3541120

null
name = stu9 address = neu1 sex = male age = 20
name = stu2 address = neu2 sex = female age = 22
name = stu4 address = neu4 sex = female age = 9
name = stu4 address = neu5 sex = male age = 24
name = stu6 address = neu3 sex = male age = 23
[/quote]

请问为什么会有两个name = stu4的元素,这两个hashcode相等,equal也是true,set中应该是不应该出现两个相同元素的,请帮忙解答一下,不胜感激!

4个回答

给你个解决办法:
[code="java"]
public boolean equals(Stu obj) {

return (this.name.equals(obj.name));

}

[/code]
改为
[code="java"]
public boolean equals(Object obj) {
Stu o=(Stu)obj;

return (this.name.equals(o.name));

}

[/code]

你放入set里的是Stu 对象,而name = stu4是Stu的一个属性,set校验的是你Stu所以属性是否重复,也就是Stu是不是一样,而不是只校验某个对象的属性。如果你将Stu的所有属性都设置一样你绝对答应不出来两个一样的Stu。

[code="java"]
// 你new了两个对象,这两个对象的hashcode,equal会一样吗?
Stu stu4 = new Stu("stu4", "neu5", "male", 24);

Stu stu5 = new Stu("stu4", "neu4", "female", 9);

[/code]

而hashSet在add的时候,是去判断stu4,stu5这两个对象的。

仔细看了一下你的代码,找的了问题所在。
63. public boolean equals(Stu obj) {

64. return (this.name.equals(obj.name));

65. }

66.

67. public int hashCode() {

68. return this.name.hashCode();

69. }

你把equals 和hashCode给重写了,你去掉就OK了,用java api原始的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐