How does a Java HashMap handle different objects with the same hash code?

前端 未结 14 1537
余生分开走
余生分开走 2020-11-22 02:27

As per my understanding I think:

  1. It is perfectly legal for two objects to have the same hashcode.
  2. If two objects are equal (using the equals() method)
14条回答
  •  无人共我
    2020-11-22 03:08

    Each Entry object represents key-value pair. Field next refers to other Entry object if a bucket has more than 1 Entry.

    Sometimes it might happen that hashCodes for 2 different objects are the same. In this case 2 objects will be saved in one bucket and will be presented as LinkedList. The entry point is more recently added object. This object refers to other object with next field and so one. Last entry refers to null. When you create HashMap with default constructor

    Array is gets created with size 16 and default 0.75 load balance.

    enter image description here

    (Source)

提交回复
热议问题