Java- How to Override equals and hashcode Method in Java? - JavaTechWorld

Saturday, 25 February 2017

Java- How to Override equals and hashcode Method in Java?

Hello Everyone, In this tutorial we are going to learn easy way to override equals and hashCode method. Before implementation of equals and hashCode method let me tell you the difference between '==' operator and equals() method.

'==' Operator - 
  • It always compare object with refernce.
  • we can't compare incompatible objects, compiler throws compilation error..
  • we can use it also for computing primitive values and two nul's directly.
equals() Method -
  • It compare object either with reference or with state based on its implementation.
  • In object class it compare object with refence.
  • In subclass it compare object with reference.
  • we can compare incompatible objects using equals it return "false".
  • we can't use it for compari
    ng primitive values and two nulls directly it leads compilation error.
Now we will understand these two method via example for this i have created one Main class and one custom object which we call it as Point we will make Point class as Final to make a package private first we will understand why we have to override equals and hashCode method what are the disadvantage if we don't override equals and hashCode method.

//Main.java

package channelsUpstream.requests.npp;
import java.util.*;
/**
 * Created by namdeva on 22/04/2017.
 */
public class Main {
    public static void main(String... args) {

        List<Point> p = new ArrayList<Point>();

        p.add(new Point(1,1));
        p.add(new Point(2,2));

        System.out.println("Size " + p.size());
        Point p1 = new Point(1,1);
        if (p.contains(p1)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}

//Point.java

package channelsUpstream.requests.npp;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
 * Created by namdeva on 22/04/2017.
 */
final class Point {
    private final double x;
    private final double y;

    //will make public constructor.
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    }

Output -





//Main.java

package channelsUpstream.requests.npp;
import java.util.*;
/**
 * Created by namdeva on 22/04/2017.
 */
public class Main {
    public static void main(String... args) {

        List<Point> p = new ArrayList<Point>();

        p.add(new Point(1,1));
        p.add(new Point(2,2));

        System.out.println("Size " + p.size());
        Point p1 = new Point(1,1);
        if (p.contains(p1)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }

}

//Point.java

package channelsUpstream.requests.npp;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
 * Created by namdeva on 22/04/2017.
 */
final class Point {
    private final double x;
    private final double y;

    //will make public constructor.
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(x).append(y).toHashCode();
    }
    @Override
    public boolean equals(Object obj) {
        //If object is null return false
        if(obj==null) {
            return false;
        }
        // If both object are same return true
        if (obj==this) {
            return true;
        }
        if (obj.getClass()!=this.getClass()) {
            return false;
        }
        //Caste into point vaiable
        Point p2=(Point)obj;
        return new EqualsBuilder().append(x,p2.x).append(y,p2.y).isEquals();
    }

}

Output -



© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.