Hashmap

what is Hashmap?

Hashmap comes under collection framework and its used for storing group of data in the form of key and value.

HashMap<Integer,String> hm = new HashMap<>();
hm.put(1,"2");

Hashmap allows duplicate value but the key should be unique if the same key called again then it will replace the existing value.

Hasmap use hascode method from the object class along with the key as an argument and produce an random number based on the random number the index will be calculated for storing data.

int hash = hashCode(key);
int index = hash & (n-1);
where n = 16 Hasmap size

By default the Hashmap create an bucket of size 16 and each bucket contain four coloumn hascode,key,value and reference to next node.

if the same index is produced by hashing technique then it will result in hash collision which then result in linkedlist process with node pointing to next node.

Important note

1.null key always stored on first index since its hashcode will be zero so the index also will be zero.

2.The time complexity for get and put method is constant o(n).

3.Its uses hashcode() and equals() method for getting data based on key.If a hash collision occur mean its uses the equals() method to matches key and hashcode() to get data from linked list.