Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Write an Equality Method in Java

Tags:

java

equality

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?

like image 555
Milan Mendpara Avatar asked Jan 31 '12 16:01

Milan Mendpara


People also ask

What is difference between == and equals () method in Java?

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.

What is == used for in Java?

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.

Which one of the following is equality operator in Java 1 point >= <= != +=?

= = is the equality operator.

Why is it important to write the equals () method when writing a class?

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).


3 Answers

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.

like image 66
DwB Avatar answered Oct 09 '22 19:10

DwB


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.

like image 38
Péter Török Avatar answered Oct 09 '22 18:10

Péter Török


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.

like image 29
Milan Mendpara Avatar answered Oct 09 '22 17:10

Milan Mendpara