As per my understanding I think:
HashMap
is an array of Entry
objects.
Consider HashMap
as just an array of objects.
Have a look at what this Object
is:
static class Entry implements Map.Entry {
final K key;
V value;
Entry next;
final int hash;
…
}
Each Entry
object represents a key-value pair. The field next
refers to another Entry
object if a bucket has more than one Entry
.
Sometimes it might happen that hash codes for 2 different objects are the same. In this case, two objects will be saved in one bucket and will be presented as a linked list.
The entry point is the more recently added object. This object refers to another object with the next
field and so on. The last entry refers to null
.
When you create a HashMap
with the default constructor
HashMap hashMap = new HashMap();
The array is created with size 16 and default 0.75 load balance.
hash % (arrayLength-1)
where element should be placed (bucket number)HashMap
, then value gets overwritten.If the bucket already has at least one element, a new one gets added and placed in the first position of the bucket. Its next
field refers to the old element.
hash % (arrayLength-1)
Entry
.
If a desired element is not found, return null