equals
method we should make sure that it's:x <-> x
)x <-> y
)x <-> y && y <-> z => x <-> z
)equals
method with null value as argument should return false.equals
method is defined in the java.lang.Object
class:xxxxxxxxxx
public boolean equals(Object obj) {
return (this == obj);
}
java.util.Objects
class provides utility methods to check if two objects are equals or deeply equals (in case of arrays):xxxxxxxxxx
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
xxxxxxxxxx
public static boolean deepEquals(Object a, Object b) {
if (a == b)
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);
}
java.util.Arrays
class provides utility methods to check if two arrays are equals
(including specific overloaded methods for each primitive type).
It also provides a utility method to check if two arrays are deeply equals (array of arrays).equals
method.
The code provide an implementation of the equals
method for a superclass "P" and subclass "C".
The code also an implementation of a method (called here same) that check if a superclass and possible a subclass are equals.xxxxxxxxxx
class P {
Integer id = 1;
public Integer getId() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
P p = (P) obj;
return new EqualsBuilder().append(this.getId(), p.getId()).build();
}
public boolean same(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof P))
return false;
P p = (P) obj;
return new EqualsBuilder().append(this.getId(), p.getId()).build();
}
}
xxxxxxxxxx
class C extends P {
String code = "default";
public String getCode() {
return code;
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj))
return false;
C c = (C) obj;
return new EqualsBuilder().append(this.getCode(), c.getCode()).build();
}
@Override
public boolean same(Object obj) {
if (!super.same(obj))
return false;
if (!(obj instanceof C))
return false;
C c = (C) obj;
return new EqualsBuilder().append(this.getCode(), c.getCode()).build();
}
}