Java - HashMap confusion about collision handling and the get() method

前端 未结 5 1854
说谎
说谎 2021-01-13 15:28

I\'m using a HashMap and I haven\'t been able to get a straight answer on how the get() method works in the case of collisions.

Let\'s say

5条回答
  •  不知归路
    2021-01-13 15:44

    Let's say n > 1 objects get placed in the same key. Are they stored in a linked list? Are they overwritten so that only the last object placed in that key exists there anymore? Are they using some other collision method?

    There could be single instance for the same key so the last one overrides the prior one

    Map map = new HashMap();
    map.put("a", 1);
    map.put("a", 2);// it overrides 1 and puts 2 there
    

    chaining comes where there turns the same hash for different keys


    See

    • Java papers hash table working

提交回复
热议问题