我正在修改即将到来的考试,我有信心说我很可能会得到一个问题,需要我实现equals方法,compareTo方法和hashCode方法。这是一本开放的书,因此我想尝试并充分利用这一点,因为我认为它对这个领域特别有用。例如,我设法通过跟随别人的代码然后调整它来实现compareTo方法,我希望在考试中有这个优势,因为其他人会=)
无论如何,我有一个样本考试问题给我们,它问我们这个问题。我正在尝试实现equals方法,同时使用其他人的代码作为示例,因为我会将这些作为模板带入我们,以防它们出现,因为它是允许的。但是,equals方法似乎不像compareTo那样通过模板实现。我已经尝试了但它看起来并没有变得很好= D我希望你们能帮助我解决我出错的地方,我想虽然我不确定它可能是我的If声明。最后,如果您在开卷考试中对上述任何方法有任何提示,那么我将非常感激!因为我真的很担心/不确定我是否会通过=)
无论如何,我得到了以下代码:
public class Books {
private final String title;
private final String author;
private final int edition;
public Books(String title, String author, int edition)
{
this.title = title;
this.author = author;
this.edition = edition;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public int getEdition(){
return edition;
}
使用这段代码,我将实现equals方法,这是我在追踪别人时所做的尝试。
public boolean equals(Object obj){
if (this == obj)
return true;
if (obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Books other = (Books) obj;
if (getTitle() == null){
if (other.getTitle() != null)
return false;
}
else if
(!getTitle().equals(other.getTitle()))
return false;
return true;
else if (getAuthor() == null){
if (other.getAuthor() != null)
return false;
}
else if
(!getAuthor().equals(other.getAuthor()))
return false;
return true;
if (getEdition() != other.getEdition())
return false;
}
我知道我几乎搞砸了if语句是如何流动的,我努力追随它,因为它的成长= /
答案 0 :(得分:1)
假设我们正在使用Java 15(已激活所有预览功能),我的实现将如下所示:
public boolean equals( Object obj )
{
var retValue = this == obj;
if( !retValue && (obj instanceof Book other) && (getClass() == obj.getClass()) )
{
retValue = Objects.equals( getTitle(), other.getTitle() );
retValue &= Objects.equals( getAuthor(), other.getAuthor() );
retValue &= getEdition() == other.getEdition();
}
return retValue;
}
如果类Book
是最后的类,则可以省略检查… && (getClass() == obj.getClass())
而不会产生不同的结果(直到有人进行更改并将Book
扩展到Atlas
为止–没有检查,Atlas
可以等于Book
,但是Book
不能等于任何Atlas
,这会损害对Object.equals()
的任何实现的要求需要对称)。
当然,retValue &= …
构造可以仅用… && …
代替。
有趣的是:Java集合类不检查该类:
List<String> a = new ArrayList<>();
List<String> l = new LinkedList<>();
List<String> e = Collections.emptyList();
out.println( a.equals( l ) ); // true
out.println( a.equals( e ) ); // true
out.println( l.equals( a ) ); // true
out.println( l.equals( e ) ); // true
out.println( e.equals( a ) ); // true
out.println( e.equals( l ) ); // true
e.getClass().getName()
将返回(对于Java 15)java.util.Collections$EmptyList
。因此,对称性不一定要求两个对象都具有相同的实现。
对于java.util.List
,甚至在文档中也提到:“ […] two lists are defined to be equal if they contain the same elements in the same order.”
答案 1 :(得分:0)
这是Eclipse为该类生成的内容:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Books other = (Books) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (edition != other.edition)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
答案 2 :(得分:0)
要覆盖用户对象特定的equals方法,我们通常应该执行以下操作,我的注释在行
@Override
public boolean equals(Object obj) {
//check if object received is not null
if (obj == null) {
return false;
}
//check if the object received is of the same class
if (getClass() != obj.getClass()) {
return false;
}
final Beta other = (Beta) obj;
//compare all properties of the class and return true based on oure requirement
if ((this.getSample() == null) && (other.getSample() == null)){
return true;
}
if ((this.getSample().getId().equals(other.getSample().getId())) && (this.getSample().getName().equals(other.getSample().getName()))) {
return true;
}
//if nothing suits well based on our requirement we can directly send false or handle the responsibility of comparison to super class
return super.equals();
}