Item 10: Obey the general contract when overriding equals
The easiest way...
The easiest way to avoid problems is not to override the equals method, in which case each instance of the class is equal only to itself. This is the right thing to do if any of the following conditions apply:
- Each instance of the class is inherently unique.
- There is no need for the class to provide a "logical equality" test.
- A superclass has already overridden equals, and the superclass behavior is appropriate for this class.
- The class is private or package-private, and you are certain that its equals method will never be invoked.
When a class is value class, overriding the equal method is appropriate. Few contracts you must adhere.
- Reflexive
x.equals(x)
must betrue
; - Symmetric:
x.equals(y)
y.equals(x)
- Transitive:
x.equals(y)
andy.equals(z)
x.equals(z)
- Consistent: if
x.equals(y)
returnstrue
, then any times of invocation will return the same value - For any non-null reference value x,
x.equals(null)
must returnfalse
- Liskov Substitution principle: any important property of a type should also hold for all its subtypes so that any method written for the type should work equally well on its subtypes. Basically, a subclass is still a superclass and must ack like as one.
Item 11: Always override hashcode when you override equals
You must override hasCode in every class that overrides equals.
Item 12: Always override toString
Providing a good
toString
implementation makes your class much more pleasant to use and makes systems using the class easier to debug.
Item 13: Override clone judiciously
In practice, a class implementing Cloneable is expected to provide a properly functioning public clone method.
immutable classes should never provide a clone method. Because it's encouraging wasteful copying.
Item 14: Consider implementing Comparable
compareTo
is declared inComparable
interface. It is required when sorting is needed for this class.
Use comparator for comparing:
private static final Comparator<PhoneNumber> COMPARATOR =
comparingInt((PhoneNumber pn) -> pn.areaCode)
.thenComparingInt(pn -> pn.prefix)
.thenComparingInt(pn -> pn.lineNum);
public int compareTo(PhoneNumber pn) {
return COMPARATOR.compare(this, pn);
}