1

Let us consider HashMap, which employs separate chaining to resolve hashcode collisions.

If I have multiple entries, where hascode comes out to be same, the collision mechanism forms a linked-list chain of all those entries.

Now, lets consider a case, where such linked list is present as:

(K1,V1,->) (K2,V2, ->) (K7,V7,->) (K9,V9,)

Now a new entry is coming in, for which the hashcode is coming as same, and the key has same value as K7. Will it overwrite the existing value of K7?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Mandar Kale
  • 337
  • 2
  • 13
  • 2
    Yes it will since if the hashcode is equal and you're operating on the collision list, `equals()` will kick in, i.e. the keys in the list will be checked for equality with the new key. Hence if you add a new value for a key that is equal to K7 then that value will be replaced. - Btw, that's the reason why hashCode() and equals() should always be implemented/overridden together. – Thomas Sep 13 '16 at 12:31
  • Depends on the implementation. As @KevinEsche said, a List can contain duplicates, so it's probably ok. Why don't you just try it? – Bene Sep 13 '16 at 12:32
  • That's clearly stated in the docs. A new entry with the same key will overwrite the old entry. – Martin Nyolt Sep 13 '16 at 12:32
  • 2
    This qustion is tagged with `hashmap`. That should be a hint. – Marko Topolnik Sep 13 '16 at 12:32
  • I don't think there's a linked list chain built into java.util.HashMap. If you add a new entry with an existing key it'll overwrite the one that's there. – duffymo Sep 13 '16 at 12:33
  • 2
    @duffymo of course there is a list of all entries with identical hash values. – Martin Nyolt Sep 13 '16 at 12:34
  • 3
    In the modern `HashMap` there's even a binary search tree. It improves the behavior in bad cases wit a lot of hashcode collisions. – Marko Topolnik Sep 13 '16 at 12:34
  • "A new entry with the same key will overwrite the old entry." - you contradict yourself. This suggests that many different keys will have identical hash codes. That's a very different problem. I'd fix the hashCode and equals functions. – duffymo Sep 13 '16 at 12:35
  • @duffymo there is a form of linked list being used for collision (just not `LinkedList` but directly implemented in `HashMap`) and when the node doesn't use a tree instead. In both cases only the value will be replaced if a node/entry already exists for the given key. – Thomas Sep 13 '16 at 12:45
  • HashMap is not a multi-map. As far as I know it doesn't allow multiple values for the same key. That's all I'm saying. If that's required, the generic value type should be a collection of some kind. The user has to manage adding values. – duffymo Sep 13 '16 at 12:47
  • "...will it overwrite the existing Value of K7" - if you by any chance mean that whether the key itself is being replaced, i.e. whether the key would be another instance after the put operation, then the answer would be no. If an existing entry is found just the value is replaced. – Thomas Sep 13 '16 at 12:47
  • @duffymo ok that might be a misunderstanding then. The OP is talking about hash collision, i.e. if mulitple keys are mapped to the same bucket. In that case either a list or a tree is used to detect the one entry that matches the given key. – Thomas Sep 13 '16 at 12:49

2 Answers2

5

Yes, it will overwrite the value reference within the existing node that represents K7.

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/HashMap.java#655

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • While it's definately fun and interesting to contemplate the inner workings of Hashmap; this question basically comes down to 'will hashmap overwrite the value if I use the same key twice', and as per the map contract, the answer to that is yes. – Joeri Hendrickx Sep 13 '16 at 12:54
  • 1
    There are many ways to meet the contract and only one of them is replacing the `value` reference of an existing node in the hash collision chain. The question is quite clear in asking about the implementation detail. – Marko Topolnik Sep 13 '16 at 13:00
0

Definition of hashmap function public V put(K key, V value) explains about the resolve for hash-collision.

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

Snippet of putVal() which is called by put()

633             if (p.hash == hash &&
634                 ((k = p.key) == key || (key != null && key.equals(k))))
635                 e = p;
...
652             if (e != null) { // existing mapping for key
653                 V oldValue = e.value;
654                 if (!onlyIfAbsent || oldValue == null)
655                     e.value = value;
656                 afterNodeAccess(e);
657                 return oldValue;
658             }
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79