Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What will happen if we put a key object in a HashMap which is already there ?

user-image
Question added by Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
Date Posted: 2014/05/24
Ahsan Ullah
by Ahsan Ullah , Lead Software Consultant , HSBC

here the concept wil go bit more narrow, this is dependent over the implementation of HashMap,

The Key Object must follow the contract of HashCode and Equals method.

 

For Specific Bucket of HashMap we get by HashCode and if the value Exist aleady than there should be the use of equals method to find the value in the bucket.

 

So further down to bucket the collision will be handled on the base of object equality.

As the bucket is implemented using LinkedList than the Equal KeyObject will be Updated otherwise new object can be added to the bucket.

Mohammed Ameen
by Mohammed Ameen , Java Developer , TATA Consultancy Services Limited (TCSL)

If the map previously contained a mapping for this key, the old value is replaced by the specified value.

 

Explanatation:

 

public V put(K key, V value)

 

Associates the specified value with the specified key in the map . If the map previously contained a mapping for this key, the old value is replaced by the specified value.

The map simply drops its reference to the OLD VALUE. If nothing else holds a reference to the object(OLD), that object becomes eligible for GARBAGE COLLECTION.

It will replace the old value.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.