Java Equals()方法帮助
我需要为teams类编写一个与提供的hashcode方法一致的equals()方法Java Equals()方法帮助,java,Java,我需要为teams类编写一个与提供的hashcode方法一致的equals()方法 public int hashCode() { return this.getPro().hashCode() + this.getTeam().hashCode(); } 哈希码方法 public int hashCode() { return this.getPro().hashCode() + this
public int hashCode()
{
return this.getPro().hashCode()
+ this.getTeam().hashCode();
}
哈希码方法
public int hashCode()
{
return this.getPro().hashCode()
+ this.getTeam().hashCode();
}
我的方法是相等的,但行不通
public boolean equals(Object obj)
{
ClassName pro = (ClassName) obj;
return (this.getPro().hashCode() == pro.getPro());
(this.getTeam().hashCode() == pro.getTeam());
}
任何帮助都是好的
&&
public boolean equals(Object obj)
{
ClassName pro = (ClassName) obj;
return this.getPro() == pro.getPro() && this.getTeam() == pro.getTeam();
}
但是,您的hashCode()
不会生成好的散列,并且在许多情况下equals()
会失败(例如,与非类名
或null相比)。有关如何正确实施它们的信息,请参阅。假设没有派生类,请尝试
@Override public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof ClassName)) return false;
ClassName pro = (ClassName)obj;
<sometype> thisPro = getPro();
if (thisPro == null || !thisPro.equals(pro.getPro()) return false;
<sometype> thisTeam = getTeam();
if (thisTeam == null || !thisTeam.equals(pro.getTeam()) return false;
return true;
}
@覆盖公共布尔等于(对象obj){
如果(obj==this)返回true;
如果(!(obj instanceof ClassName))返回false;
ClassName pro=(ClassName)obj;
thisPro=getPro();
如果(thisPro==null | |!thisPro.equals(pro.getPro())返回false;
thisTeam=getTeam();
如果(thistTeam==null | |!thistTeam.equals(pro.getTeam())返回false;
返回true;
}
<代码> > p>这里,将hash码(int)与对象进行比较。此外,在语句的中间有一个分号。
您应该尝试以下方法:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyClass myClass = (MyClass) o;
if (!pro.equals(myClass.pro)) return false;
if (!team.equals(myClass.team)) return false;
return true;
}
这里比较对象的内容
@Bart K.comment之后,如果team或pro可以为null,那么这里是编写equals()
方法的方法:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyClass myClass = (MyClass) o;
if (pro != null ? !pro.equals(myClass.pro) : myClass.pro != null) return false;
if (team != null ? !team.equals(myClass.team) : myClass.team != null) return false;
return true;
}
资源:
关于同一主题:
您正在将hashCode与对象进行比较。这是行不通的。您想实现什么?您认为hashCode()是什么是吗?如果要将对象放入HashSet或HashMap,那么两个相等的对象必须返回相同的hashCode,而两个返回不同hashCode的对象不能相等。两个不相等的对象可以返回相同的hashCode(例如,可能有40亿个hashCode,但可能有更多的字符串)。您不能用hashCode()来定义equals();它可能会说两个不相等的对象是相等的。equals(…)
还应该检查obj==null
。这取决于pro和team是否可以为null。因为它们似乎不可以为null(参见OPhashCode()
)不,我不是说pro
或team
为空,而是说obj
。例如,ArrayList
可以包含null
引用,如果你做了包含(instanceOfClassName)
在它上面,当比较null
和this
时,你的实现会抛出一个NPE。糟糕的是,你是对的,我用了一个更好的equals方法编辑了代码。感谢你没有把this
放在方法调用前面。我不会用=
来比较equals()中的对象
method。大多数情况下(如果不总是),如果对象的内容相等,那么对象本身也是相等的。条件(obj==null)是冗余的:(null instanceof ClassName)=false@Colin:Oops我假设==
比较值而不是引用。已修复。什么是“等于”平均值完全取决于您尝试执行的操作。例如,您可能完全基于ID字段而不考虑任何其他内容。通常,如果两个对象都采用相同的概念(如6/8等于3/4(音乐中除外)),则两个对象是相等的如果你有两个对象,你把它们都放在一个HashSet中,你希望这个集合只包含其中一个,而你不在乎哪个,那么这两个对象是相等的。