Consider adding an equality method to the following class of simple points:
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
// ...
}
// my definition of equals
public boolean equals(Point other) {
return (this.getX() == other.getX() && this.getY() == other.getY());
}
What's wrong with this method? At first glance, it seems to work OK:
Point p1 = new Point(1, 2);
Point p2 = new Point(1, 2);
Point q = new Point(2, 3);
System.out.println(p1.equals(p2)); // prints true
System.out.println(p1.equals(q)); // prints false
However, trouble starts once you start putting points into a collection:
import java.util.HashSet;
HashSet<Point> coll = new HashSet<Point>();
coll.add(p1);
System.out.println(coll.contains(p2)); // prints false
How can it be that coll does not contain p2, even though p1 was added to it, and p1 and p2 are equal objects?
In java both == and equals() method is used to check the equality of two variables or objects. == is a relational operator which checks if the values of two operands are equal or not, if yes then condition becomes true. equals() is a method available in Object class and is used to compare objects for equality.
Summary. 1) use == to compare primitive e.g. boolean, int, char etc, while use equals() to compare objects in Java. 2) == return true if two references are of the same object. The result of the equals() method depends on overridden implementation. 3) For comparing String use equals() instead of == equality operator.
= = is the equality operator.
The reason the equals method in the Object class does reference equality is because it does not know how to do anything else. Remember, every class in Java is an Object (via inheritance).
While it is true that you should implement hashCode()
when you implement equals()
, that is not causing your problem.
That is not the equals()
method you are looking for. The equals method must always have the following signature: "public boolean equals(Object object)". Here is some code.
public boolean equals(Object object)
{
if (object == null)
{
return false;
}
if (this == object)
{
return true;
}
if (object instanceof Point)
{
Point point = (Point)object;
... now do the comparison.
}
else
{
return false;
}
}
The Apache EqualsBuilder class is useful for equals implementations. The link is an older version, but still applicable.
If you liked the Apache EqualsBuilder, you will probably like the Apache HashCodeBuilder class as well.
Edit: updated the equals method example for standard shortcuts.
You must implement hashCode()
whenever you override equals()
. These two work together, and they must give consistent results at all times. Failing to do so produces just the erroneous behaviour you observed.
This is explained in more detail e.g. in Effective Java 2nd Edition, Item 9: Always override hashCode when you override equals.
Works well by overriding hashcode !
Always remember : override hashCode when you override equals.
@Override public int hashCode() {
return (41 * (41 + getX()) + getY());
}
This is my implementations of hashCode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With