How HashMap works in Java?
It is a simple answer,it works on the principle of hashing.In HashMap elements are stored in pair of (key,value) and also we can fetch the value in constant time O(1) provided we know the key.We store objects in HashMap using put(key,value) and retrieve value using get(key) which returns value.
Working of put(key,value):When we want to add a element in the HashMap,we provide the key value pair.Hashmap calculates the hashcode for the given key and after that it uses the hashing function to get the location in bucket array and then at that location it stores the key,value pair.Yes you are reading right.Key and Value both are stored in the bucket.But suppose more than one key is having the same hashcode then what will happen.Since if hashcode is same then the location will be same.For this we can use collision resolution techniques.HashMap stores elements in the form of linked list.So if there is collision,an extra node is added and that key value pair will be stored at that address.So in this way we can overcome with this problem.Now you can ask next question how will we fetch the exact data if the location contains more than one key,value pair.So in this case,Since we know bucket stores key and value both and when we call get(key) then the value will be searched and if the location contains more than one key,value pair then key will be compared with equals() method and then the right value will be extracted.So in this way HashMap insertion and extraction work.
What happens if the size of hashmap exceeds the threshold defined by load factor?
When this happens it will act to resize the hashmap by creating a new bucket array of twice the size of previous hashmap. After that it will it will start putting every old element from old array bucket to new array bucket.This process is alled rehashing because when it copies the data to new array bucket first it applies the hash function to get new bucket location.
There can arise a problem.Suppose two threads at the same time sees that the hashmap bucket is full and has to be resized then both these thread will be resizing the bucket array and will produce a race condition.
It is a simple answer,it works on the principle of hashing.In HashMap elements are stored in pair of (key,value) and also we can fetch the value in constant time O(1) provided we know the key.We store objects in HashMap using put(key,value) and retrieve value using get(key) which returns value.
Working of put(key,value):When we want to add a element in the HashMap,we provide the key value pair.Hashmap calculates the hashcode for the given key and after that it uses the hashing function to get the location in bucket array and then at that location it stores the key,value pair.Yes you are reading right.Key and Value both are stored in the bucket.But suppose more than one key is having the same hashcode then what will happen.Since if hashcode is same then the location will be same.For this we can use collision resolution techniques.HashMap stores elements in the form of linked list.So if there is collision,an extra node is added and that key value pair will be stored at that address.So in this way we can overcome with this problem.Now you can ask next question how will we fetch the exact data if the location contains more than one key,value pair.So in this case,Since we know bucket stores key and value both and when we call get(key) then the value will be searched and if the location contains more than one key,value pair then key will be compared with equals() method and then the right value will be extracted.So in this way HashMap insertion and extraction work.
What happens if the size of hashmap exceeds the threshold defined by load factor?
When this happens it will act to resize the hashmap by creating a new bucket array of twice the size of previous hashmap. After that it will it will start putting every old element from old array bucket to new array bucket.This process is alled rehashing because when it copies the data to new array bucket first it applies the hash function to get new bucket location.
There can arise a problem.Suppose two threads at the same time sees that the hashmap bucket is full and has to be resized then both these thread will be resizing the bucket array and will produce a race condition.
0 comments:
Post a Comment