Internal Working and implementation of hashmap and hashset | Java Interview Questions | Code Decode

preview_player
Показать описание
In this video of Java Interview Question and Answer series we have explained internal working of hashmap and hashset which is important question in Java interview questions and answers series

Udemy Course of Code Decode on Microservice k8s AWS CICD link:

Course Description Video :

We need to cover 3 min points in High level for this question:
It works on the principle of hashing
How Put Method works Internal working
How Get method Works internally

Hash Map internally works on the principle of Hashing.

Hashing means using some function or algorithm to map object data to some integer value, hashCode() method return you that hash code. Hence Its necessary to write hashCode() method properly for better performance of HashMap.

If you override hashCode() method, it’s necessary to fulfill Equals and Hashcode Contract.

MAP is an object that maps keys to values”

So, there must be some mechanism in HashMap to store this key-value pair.

Everything in Hashmap is stored in a bucket internally (of hash table - underling DS)

Firstly hash value is calculated using the key’s hash code by calling its hashCode() method. This hash value is used to calculate the index in the array for storing Entry object. JDK designers well assumed that there might be some poorly written hashCode() functions that can return very high or low hash code value. To solve this issue, they introduced another hash() function and passed the object’s hash code to this hash() function to bring hash value in the range of array index size.

With Hash Code in place , we put the newly created Entry object K,V in the bucket of Hash Table.

So, in case of collision, Entry objects are stored in linked list form. When an Entry object needs to be stored in particular index, HashMap checks whether there is already an entry?? If there is no entry already present, the entry object is stored in this location.

If there is already an object sitting on calculated index, its next attribute is checked. If it is null, and current entry object becomes next node in linkedlist. If next variable is not null, procedure is followed until next is evaluated as null.

What if we add the another value object with same key as entered before. Logically, it should replace the old value. How it is done? Well, after determining the index position of Entry object, while iterating over linkedist on calculated index, HashMap calls equals method on key object for each entry object.

We know that two unequal objects can have the same hash code value. This is a case of collision.

Hash collisions have negative impact on the lookup time of HashMap. When multiple keys end up in the same bucket, then values along with their keys used to be placed in a linked list. In case of retrieval, linked list has to be traversed to get the entry. In worst case scenario, when all keys are mapped to the same bucket, the lookup time of HashMap increases from O(1) to O(n).

Java 8 has come with the following improvements/changes of HashMap objects in case of high collisions.
The alternative String hash function added in Java 7 has been removed.
Buckets containing a large number of colliding keys will store their entries in a balanced tree instead of a linked list after certain threshold is reached.

-------------------------------------------------------------------------------------------------------------------------------------
Subscriber and Follow Code Decode

--------------------------------------------------------------------------------------------------------------------------------------

#internalworkinghashmap #javainterviewquestions #codedecode
Рекомендации по теме
Комментарии
Автор

Really liked the idea of explaining through slides also.
And thanks for explaining the functionality for Java 7 and Java 8 separately; insightful indeed.

rahulagrawal
Автор

Thank you so much @code decode guys for your support. I cleared multiple interviews with your valuable videos. Hats off

vijayavinayaktandur
Автор

The Best Video to understand Internal working of HashMap. Thanks a lot ❤

sanyasee
Автор

Hi Mam, All you videos are excellent and very helpful to clear interviews. Your are doing this video at 1 AM... Hatsoff to your dedication

ssbunny
Автор

Thank you for taking us through the source code of these classes. I used to be afraid of checking such files. But this is how we get to learn good coding practice also.

shekhar_sahu
Автор

Best video I've ever seen about HashMap's working!

shreyashachoudhary
Автор

Hi Mam! You are helping us a lot. Please keep teaching like this And also if possible please make a complete session on Collections frame work end to end. There are many tutorials on youtube, but nobody can teach us the way you do. I found your tutorials very helpful and easiest way to understand.

saivamsi
Автор

I was searching for this type of video for a long time.
This video is very informative and helped me better understand the concepts.
Thanks for sharing such insightful content.

rajatgoyal
Автор

This channel is underrated! Great work.

smitchaudhari
Автор

You delivered a nice explanation. It was needed for interview because it is important. I found this video and it helped me out. Thanks.

grryeth
Автор

Excellent explanation of internals of hashmap as well as hasheet with proper proof

ArunSharma-hutd
Автор

Thankyou so much dear❤ your videos are good to go for interview topics.. crisp and perfect .. Interviewer bhi khush hojaye😂

akankshasinha
Автор

Thanks Mam for such à deep knowledge. God Bless you. Keep posting 🎉

kaushalchandra
Автор

Simple and accurate explaination - good job

anison
Автор

13:32 “while converting the list to binary hashcode is used as branching variable” - couldn’t get this part as hashcode is same then they are getting converted to linked list and then equals method is being used to add the values in the linked list. So the question is if it has reached a certain threshold and then how different values of hashcode can come to get converted into tree?

shivamanand
Автор

Ur videos are too good to prepare for interview. I have a question here. Whe linked list is converted into binary tree then which hash code is used to determine the left or right node? As per my understanding hash code of all keys is same because of which collision is happening so now which hash code to use for node calculation?

reshusinghal
Автор

Wonderful video explaining exactly what changed with Java8. Can you please create one for ConcurrentHashMap as well

aniketkalamkar
Автор

Too good of explanation :) thanks lot - great work - keep going

raghuakuthota
Автор

The content is really great, explaining everything in details which makes the internal working of HashMap and HashSet very clear. However, I have one question. Do we need this understanding for programming in real projects as generally we need to use HashMaps and HashSets to store and retrieve collection data and can also iterate using iterators, or is this just for interview purpose ?

yasirakhn
Автор

Have been checking all your videos..Inspiring work and content!!!

divyavegoti