ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
تاريخ النشر: 2014/05/24
Ahsan Ullah
من قبل 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
من قبل 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.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟