significance of equals and hashcode

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

If you are preparing for java certification exam, then while studying hashcodes and collections, you must have encountered one statement. Here we are going to learn about equals and hashcode methods in java.

If you are overriding equals() method then also override hashcode() method because all the data structures that use hashing, use hashcodes for considering object equality. If the hashcode of two objects are not equal, then objects cannot be equal.


What exactly this statement mean? We will directly try to implement by a program. We have a Bike class which of course can create Bike objects.

We will create bikes in a different class and try to add them into HashSet. The criteria for our bikes to be equal is that their engine capacity and color should be same. If they are same, they are equal bikes. Thats why we have overridden equals method accordingly in the Bike class.

Now, HashSet is a data structure that stores unique elements,duplicates not allowed. Let us test this by adding equal strings in a HashSet and bikes in a different HashSet.

This outputs

For strings, HashSet behaved correctly, two strings are equal, so only one was added in it and size is 1. But what happened in case of bikes? Two bikes are meaningfully equal, as we have overridden the equals method for it. but why does HashSet is allowing duplicate elements? That means according to HashSet, they are not equal. This fails the condition of equals elements in hashset. What is wrong?

The only thing wrong about this is we are not overriding hashcode method for Bike class. As HashSet uses hashing, and objects must be equal if and only if their hashcodes are equal, we must override hashcode() for Bike class.


So let us do it now. Bike class is updated as follows.

This outputs,

This is desired output. I hope this example helps understand how HashSets use hashing for storing and retrieving objects and why is it important to override hashcode() when you are overriding equals().







Share Button

17 comments for “significance of equals and hashcode

  1. shirish
    August 28, 2013 at 11:04 am

    Best explanation why implementation of hashcode and equals is required

    • Prasad Kharkar
      August 28, 2013 at 11:06 am

      Hey, Thanks shirish, I am glad this was informative for you 🙂 Happy learning.

  2. Diwakar
    October 15, 2013 at 3:02 pm

    One of the best example I have seen. Keep it up..:). If possible provide some more collections tutorials

    • Prasad Kharkar
      October 15, 2013 at 6:44 pm

      Thank you Diwakar for stopping by and reading, I will surely write some more tutorial regarding collections. Please suggest me some topics and I will definitely look forward to write about them. Thanks again.

  3. Anand Singh
    November 21, 2013 at 11:36 am

    Great work. Appreciated. Can u write tutorial on Exception handling with inheritance in java .

    • Prasad Kharkar
      November 21, 2013 at 11:23 pm

      Thank you Anand. Can you please elaborate your exact problem? I will try my best to answer.

  4. Sudhir Kumar
    November 22, 2013 at 7:16 am

    hhmm…. filling sleepy .

  5. pallav rajput
    January 14, 2014 at 12:17 pm

    Good example prasad !! but i would like to point out something here…..
    As you mentioned in your example “For strings, HashSet behaved correctly, two strings are equal, so only one was added in it and size is 1” ????

    actually for String hashcode and equals methods are overrided by sun people.

    So, you should mentioned here “In String and all wrapper classes the hashcode and equals methods are overrided, hence, in case of string it is working fine !!

  6. Kishor
    April 11, 2014 at 8:51 pm

    Hi Prasad…

    Good Example and explanation… Keep it up…

  7. October 30, 2014 at 12:39 am

    Thanks for this great Tutorials. It helps me a lot. But can you please more elaborate because I have heard some bucket type of thing in hashcode which is used to store and compare two object according to its hexadecimal values.
    Sorry i am new to this topic so please help me…

  8. vijay
    August 14, 2015 at 3:51 am

    please, upload one example for designing the immutable class as a key for putting it into the map rather than using String and Wrapper Classes.

  9. Aditya
    April 7, 2017 at 12:55 pm

    That is what I needed.. I doubt.. is there a standard way to create hashcode . .. suppose there is class with just 2 int variable.. what should be the hashcode for it.

    • April 8, 2017 at 4:18 pm

      You can directly generate hashcode and equals methods in eclipse

Leave a Reply

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