Can we use object as a key in hashmap in Java?

We Are Going To Discuss About Can we use object as a key in hashmap in Java?. So lets Start this Java Article.

Can we use object as a key in hashmap in Java?

  1. Can we use object as a key in hashmap in Java?

    If it were mutable, then the hashcode() value or equals() condition might change, and you would never be able to retrieve the key from your HashMap.

  2. use object as a key in hashmap in Java

    If it were mutable, then the hashcode() value or equals() condition might change, and you would never be able to retrieve the key from your HashMap.

Solution 1

A simple thumb rule is to use immutable objects as keys in a HashMap.

because:

If it were mutable, then the hashcode() value or equals() condition might change, and you would never be able to retrieve the key from your HashMap.

More precisely, class fields that are used to compute equals() and hashcode() should be immutable!

Now, suppose you create your own class:

  • To compare two objects of your class you will have to override equals()
  • To use it as a key in any Hash based Data structure you will have to override hashcode() (again, keeping immutability in mind)

Remember that if two objects are equal(), then their hashcode() should be equal as well!

Original Author Sarthak Mittal Of This Content

Solution 2

hashCode() -HashMap provides put(key, value) for storing and get(key) method for retrieving Values from HashMap. When put() method is used to store (Key, Value) pair, HashMap implementation calls hashcode on Key object to calculate a hash that is used to find a bucket where Entry object will be stored. When get() method is used to retrieve value, again key object is used to calculate a hash which is used then to find a bucket where that particular key is stored.

equals() – equals() method is used to compare objects for equality. In case of HashMap key object is used for comparison, also using equals() method Map knows how to handle hashing collision (hashing collision means more than one key having the same hash value, thus assigned to the same bucket. In that case objects are stored in a linked list, refer figure for more clarity.
Where hashCode method helps in finding the bucket where that key is stored, equals method helps in finding the right key as there may be more than one key-value pair stored in a single bucket.

Original Author Niranjan Nayak Of This Content

Solution 3

You can use any object in a HashMap as long as it has properly defined hashCode and equals methods – those are absolutely crucial because the hashing mechanism depends on them.

Original Author Grzegorz Piwowarek Of This Content

Solution 4

Answer to your question is yes, objects of custom classes can be used as a key in a HashMap. But in order to retrieve the value object back from the map without failure, there are certain guidelines that need to be followed.

1)Custom class should follow the contract between hashCode() and equals().

The contract states that:

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.

This can be done by implementing hashcode() and equals() in your custom class.

2) Make custom class immutable.

Hint: use final, remove setters, use deep copy to set fields

Original Author daemonThread Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment