When
you store the values in
HashMap,
the values are actually stored in a set of buckets.
Each of those buckets has been assigned a number which is use to
identify it. HashMap
implementation calls hashCode
method on
Key object and assigns the key object hashcode number to bucket to identify it. When you put a value in the HashMap,
it stores the data in one of those buckets. Which bucket is used
depends on the hashCode
that
will return by your object.
Important
point to mention is that HashMap in Java stores both key and value
object as Map.Entry in
bucket which is essential to understand the retrieving logic.
What
will happen if two different objects have same hashcode?
equals() and hashCode() contract that
two unequal object in Java can have same hashcode
Since hashcode
is same, bucket location would be same and collision will occur in
HashMap, Since HashMap use LinkedList to store object, this
entry (object of Map.Entry comprise key and value )
will be stored in LinkedList.
No comments:
Post a Comment