equals and hashCode Contract In Java

Before understanding equals and hashCode Contract , firstly lets understand equals() and hashCode() methods :

equals() method

equals () method is available in Object class in java ,
this is how default implementation of equals method available in Object.class :

  public boolean equals(Object obj) {
     return (this == obj);
   }

By default , in String class equals method is overridden for comparing String type values.
In java , equals() method is used to check equality of 2 objects in following two ways :

1.Shallow Comparison.
2.Deep Comparison.

1.Shallow Comparison :

The default implementation of equals() method is available in java.lang.Object class ,it simply check whether 2 object references (for example , x and y) refers to same object. As per oracle documentation .For non-null references x and y ,equals() method return true ,if x and y refer to same object(x = = y ). It is also called as Shallow Comparison.

2.Deep Comparison :

lets take simple example to Understand deep comparison in java , for example , if we have a class called
Customer
with 2 states or variables such as :
customerId ,
customerName

now if , we want to compare different objects of Customer class , then we must override equals method and in that method we must write logic of comparison by using variable or states of class,such comparison which is based on states called as deep comparison.

As per java documentation , some principles of equal() method are :

The equals method implements an equivalence relation on non-null object references:
•It is reflexive: for any non-null reference value x, x.equals(x) should return true.
•It is symmetric: for any non-null reference values x and y, x.equals(y)should return true if and only if y.equals(x) returns true.
•It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
•It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
•For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects;that is, for any non-null reference values x and y,this method returns true if and only if x and y refer to the same object(x == y has the value true).

Sample equal method example :

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (obj == null || obj.getClass() != this.getClass())
			return false;

		Customer customer = (Customer) obj;
		if (customer.getCustomerId() == this.customerId && customer.getCustomerName().equals(this.customerName)) {
			return true;
		}
		return false;
	}

Important Note :Remember that it is generally necessary to override the hashCode method whenever equals method is overridden,so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

hashCode() method

hashCode() method defined in java.lang.Object class ,this method return distinct integers for distinct objects.This method is typically implemented by converting the internal address of object into an integer value.hashCode() method is declared in Object class as below :

public native int hashCode();

hashCode() method is a native method , native methods are methods , written in other language than java and can access System related function’s and APIs which are not accessible to java language. The hashCode() method is used to generate the hash values of objects. Using these hash values, these objects are stored in Java collections such as HashMap, HashSet and HashTable. sample example :

@Override
	public int hashCode() {
		return this.customerId;
	}

Now , if you understood both equals() and hashCode() method , then its time to understand , contract between them.

equals and hashCode Contract in java

As per equals() and hashCode() contract in java always follow below rules while comparing two objects :

1.If equals method return true , then hashcode value must be same.

2.if equals method return false , then hashcode value may or may not be the same.

3.if hashcode is same , then equals may or may not return true.

Understand following Example step by step :

Step 1: Create Customer Class As Below :

package equals.hashcodenew;

public class Customer {

	private int customerId;
	private String customerName;

	public Customer() {
		super();
	}

	public Customer(int customerId, String customerName) {
		super();
		this.customerId = customerId;
		this.customerName = customerName;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	@Override
	public String toString() {
		return "Customer [customerId=" + customerId + ", customerName=" + customerName + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (obj == null || obj.getClass() != this.getClass())
			return false;

		Customer customer = (Customer) obj;
		if (customer.getCustomerId() == this.customerId && customer.getCustomerName().equals(this.customerName)) {
			return true;
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return this.customerId+1111;
	}
}

Note : in above class , i have overridden method equals and hashcode methods.

Step 2 : Now Write main class as below : Understand code and analyze output :

package equals.hashcodenew;
public class Test {
	public static void main(String[] args) {
		System.out.println("this example is copyright to crtr4u.com : ");
		Customer c1 = new Customer();
		c1.setCustomerId(1);
		c1.setCustomerName("swapnil vyawhare");
		
		Customer c2 = new Customer();
		c2.setCustomerId(1);
		c2.setCustomerName("swapnil vyawhare");

		System.out.println("\n1.If equals method return true , then hashcode value must be same.");

		System.out.println("Is Customer 1 equals Customer 2 : " + c1.equals(c2));

		System.out.println("Hashcode of object 1 : " + c1.hashCode() + " and hashCode of object 2 : " + c2.hashCode());

		Customer c3 = new Customer();
		c3.setCustomerId(3);
		c3.setCustomerName("Pooja Patil");

		Customer c4 = new Customer();
		c4.setCustomerId(4);
		c4.setCustomerName("Abhi Patil");

		System.out.println("\n2.if equals method return false , then hashcode value may or may not be the same : ");
		System.out.println("Is Customer 3 equals Customer 4 : " + c3.equals(c4));
		System.out.println(
				"Hashcode of Customer 3 : " + c3.hashCode() + " and Hashcode of Customer 4 : " + c4.hashCode());

		Customer c5 = new Customer();
		c5.setCustomerId(5);
		c5.setCustomerName("Pooja Patil");

		Customer c6 = new Customer();
		c6.setCustomerId(5);
		c6.setCustomerName("Abhi Patil");

		System.out.println("\n3.if hashcode is same , then equals may or may not return true : ");
		System.out.println("Is Customer 5 equals Customer 6 : " + c5.equals(c6));
		System.out.println("Hashcode of Customer 5 " + c5.hashCode() + " hashcode of Customer 6 " + c6.hashCode());

	}
}

Output :

this example is copyright to crtr4u.com : 

1.If equals method return true , then hashcode value must be same.
Is Customer 1 equals Customer 2 : true
Hashcode of object 1 : 1112 and hashCode of object 2 : 1112

2.if equals method return false , then hashcode value may or may not be the same : 
Is Customer 3 equals Customer 4 : false
Hashcode of Customer 3 : 1114 and Hashcode of Customer 4 : 1115

3.if hashcode is same , then equals may or may not return true : 
Is Customer 5 equals Customer 6 : false
Hashcode of Customer 5 1116 hashcode of Customer 6 1116

Happy Learning,Have A Great Day Ahead.

Leave a Reply

Your email address will not be published. Required fields are marked *