Java 是否必须在hashCode()和equal()方法中包含像Hashset这样的集合类型字段

Java 是否必须在hashCode()和equal()方法中包含像Hashset这样的集合类型字段,java,collections,hashcode,Java,Collections,Hashcode,在我的项目中,我看到了在hashCode()和equal()方法中包含集合字段时的性能问题。是否必须包含?下面是示例程序 Student.java public class Student { int no; String name; String adress; Set < Parent > parent; public int hashCode() { final int prime = 31; in

在我的项目中,我看到了在hashCode()和equal()方法中包含集合字段时的性能问题。是否必须包含?下面是示例程序

Student.java

public class Student {
    int no;
    String name;
    String adress;
    Set < Parent > parent;    
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((adress == null) ? 0 : 
       adress.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + no;
        result = prime * result + ((parent == null) ? 0 : 
       parent.hashCode());
        return result;
    }   
   public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    if (adress == null) {
        if (other.adress != null)
            return false;
    } else if (!adress.equals(other.adress))
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (no != other.no)
        return false;
    if (parent == null) {
        if (other.parent != null)
            return false;
    } else if (!parent.equals(other.parent))
        return false;
    return true;
}      

}
公共班级学生{
国际贸易编号;
字符串名;
字符串地址;
设置Parent;
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果+((地址==null)?0:
address.hashCode());
result=prime*result+((name==null)?0:name.hashCode();
结果=素数*结果+否;
结果=素数*结果+((父项==null)?0:
parent.hashCode());
返回结果;
}   
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
学生其他=(学生)obj;
如果(地址==null){
if(other.address!=null)
返回false;
}如果(!地址等于(其他地址))
返回false;
if(name==null){
if(other.name!=null)
返回false;
}如果(!name.equals(other.name))
返回false;
如果(否!=其他。否)
返回false;
如果(父项==null){
if(other.parent!=null)
返回false;
}如果(!parent.equals(other.parent))
返回false;
返回true;
}      
}
对于如何实现
equals()
hashCode()
没有“强制性”要求。嗯,有一个小要求我会在下面提到。您可以以对应用程序有意义的任何方式实现它们。看来你只需要考虑学生的身份证号码,因为这就是学生的独特之处。当然,名字和父母不是唯一的

只需确保
equals()
hashCode()
都基于相同的字段,以便您履行所需的合同(这是唯一的“必需”方面)。

对于如何实现
equals()
hashCode()
没有“强制性”要求。嗯,有一个小要求我会在下面提到。您可以以对应用程序有意义的任何方式实现它们。看来你只需要考虑学生的身份证号码,因为这就是学生的独特之处。当然,名字和父母不是唯一的


只需确保
equals()
hashCode()
都基于相同的字段,以便您履行所需的合同(这是唯一的“必需”方面)。

这不是强制性的,但是重写这些方法将使您能够控制比较结果请参见:这不是强制性的,然而,重写这些方法将使您能够控制比较结果。请看:我认为您应该比“基于相同字段”更精确一些:它们应该是一致的,这样两个
equals
为真的对象也有equal
hashCode
s。嗨,Jim,我明白你的意思了,我的问题是,如果我们在hashCode()和equals()方法中包含SetParent,那么会降低性能。我们在应用程序中面临这个问题。从hashCode()和equals()方法中删除与集合相关的变量后,性能得到了提高。这是正确的吗?是的,没有必要在比较中包括
集合。我认为你应该比“基于相同的字段”更精确一些:它们应该是一致的,这样
等于
的两个对象也有相等的
hashCode
s.Hi Jim,我明白你的意思了,我的问题是,如果我们在hashCode()和equals()方法中包含SetParent,那么会降低性能。我们在应用程序中面临这个问题。从hashCode()和equals()方法中删除与集合相关的变量后,性能得到了提高。这是否正确?是的,比较中不需要包含
集。