Tuesday, 18 August 2020

How HashMap Works in Java

How HashMap Works in Java 


HashMap is a class in Java as a part of collection framework . HashMap used to store object in  the form of key value pair .

where key and value both are objects. 

To store a object in HashMap it have following method 

public V put(K key V Value){}

To store a Object we have to create an object of HashMap class when we do this it create a array  .
each object strorage location in the array called bucket and in order to store object at bucket location.

in HashMap use hashcode of key object which is calculated by hashing .

hashcode value generated by hash function of key object also use provide name of bucket in which object store .
  
as we know two different object can have same hashcode in that situation same hashcode bucket location  contain a LinkList 

in that list HashMap store all same hashcode objects .

To get the value from HashMap 


To get an object from HashMap require key name of Object and use following get method

public V get(K key)

The Get method first calculate the hashcode of key object to find out the bucket address for getting object from bucket

if more the one object found for same hashcode in particuler bucket location in  the LinkList

equals method use for key object to checked the equality of object with stored object key if it return true then 
the corresponding object is return by get method .


No comments:

Post a Comment