Skip to main content

Understanding Equals and hashCode contract,and what can go wrong

SO what about Equals and hashcode?
Most people will say these are the two basic function available in Object class and could be called from any Class object created in Java. Well thats true but there is more then this.
First take a look at HashCode:
Every JVM provides a good hashCode implementation mostly based on the object's memory location and for two different object(by location) it produce different hashCode.
And at the same time Equal method also consider two different memory location object as Not Equal. So at core two are in a contract and works great.

But as soon as start writing code and write a class and create a state of a class(means create few data variables in a class), the core contract of hashCode and Equal doesnt work any more.

Class MyFirstCLass{
int value;
public MyFirstCLass(int value){
this.value = value;

MyFistCLass obj1 = new MyFirstCLass(10);
MyFistCLass obj2 = new MyFirstCLass(10);

Now obj1 and obj2, theoratically should be equal as both holds the same value. Bu the basic implemenataion of equals method in java will say they are not and hashCode will also produce different hashCode value for them(basically confirms that they are not same)

Now i write following code for hash based collection.

Set mySet = new HashSet();

now if i do following
mySet.contains(obj2) //see obj2 and not obj1
it returns false and basically my code doesnt work.

So then we say ok we need to write our own hashCode method, because above code should have worked.
Now Java has said that you should and can implement hashCode and Equal functions but there are few rules you should take care of
    The equals method implements an equivalence relation:
  • It is reflexive: for any reference value x, x.equals(x) should return true.
  • It is symmetric: for any 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 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 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 object is modified.
  • For any non-null reference value x, x.equals(null) should return false.
 and The general contract of hashCode is:
  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
If you want to squeeze out one sentence out of everything i have written then contract between two is
Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.
(Note:  However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.)

Things to consider when writing equal method
  1. Always check if they are at same memory location by using == operator on two objects
  2. Two different class objects can not be considered equals, so always check if passed object is instance of same class.
  3. Then compare object's property and see if they are equal.
  4. Even if you are 100% sure that your hashCode returns unequal hashCode for unEqual object, still never use it here from performance point of view.
What happens when hashCode and equals break contracts
  1. hashCode return same Value for un equal object. The performance of hashBased collection will go down as there will be more collision.
  2. hashCode return different Value for equal objects. You may not be able to find the exisitng object from hash based collection.


Popular posts from this blog

Difference between ArrayList and LinkedList

What is the difference between ArrayList and LinkedList

First of all lets see what Interfaces they implement.
ArrayList (See on Oracle/sun site)SerializableCloneableIterable<E>Collection<E>List<E>RandomAccessLinkedList(See on Oracle/Sun site)SerializableCloneableIterable<E>Collection<E>Deque<E>List<E>Queue<E>By looking at interface being implemented by these two collection, we could see RandomAccess is implemented by ArrayList and Queue<E>,Deque<E> implemented by LinkedList only. So all the differences between these two collections are because of these three Interfaces and their implementation.

RandomAccess : ArrayList can be accessed randomly, means we could access nth element of an ArrayList directly without affecting search/seek performance. e.g. list.get(n). Means get performance of nth element will never depend on how large is the ArrayList.It will remain constant.
While LinkedList cant be accessed randomly(though it does …

Validate HTML as XHTML in Intelij

When working in intelij with HTML, sometimes we need to make sure our HTML is XHTML compliance, no un ened tags etc.
i.e. we should not have

<img src="myimage.jpg> If You put above code in an HTML and run it in browser it will work fine, but if you are using some server side framework like Thymeleaf with Spring, it will generate error saying <img> tag must be terminated.

By Default intelij will not complaint about this and when you are writing HTML you may ignore the error and will get the error when you actually run it with in server. Or worst some HTML developer is writing the code HTML code for you and then you will have to fix all these errors.

Intelij can help you to make sure you write a valid HTML(XHTML). You can enable XHTML validation for HTML files by following below steps.

Go to Settings(Windows)/Preference(mac) in Intelij.Search for "File Types"Find HTML as shown in image below. You will see HTML has some extension associated with like *.html, *…