Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
Date de publication: 2014/05/24
Ahsan Ullah
par 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
par 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.

Utilisateur supprimé
par Utilisateur supprimé

It will replace the old value.

More Questions Like This