Java – bad practice – the class defines CompareTo (…) and uses object equals()

Want to know what needs to be done with the listed methods

public final int compareTo(final FieldDTO o) {
        return o.available.compareTo(this.available);

It throws an exception on line 2. Bad practice - the class defines CompareTo (...) and uses object Equals() 16 days field defines CompareTo (fielddto) and uses object equals()

I don't know what I should do Thank you in advance

Solution

If you define CompareTo, you should at least define equals

boolean equals(it) { 
  return compareTo(it) == 0; 
}

When you put objects in maps and collections, you will encounter strange problems It is also generally good practice to define hashcode

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>