How HashMap Works Internally?

  Рет қаралды 72,671

Siva Reddy

Siva Reddy

7 жыл бұрын

Please check out my blog(learnsimple.in) for more technical videos.
In this video, I explained How HashMap work internally? This is one of the important interview Q&A for experience candidate.
For any java/full stack/DevOps/developer/lead positions related mock interviews assistance/Guidance/support, you can reach out me @ sivajavatechie5@gmail.com

Пікірлер: 66
@josephkumar9112
@josephkumar9112 3 жыл бұрын
Thank you For the Video...🙂
@SivaReddyJavaTechie
@SivaReddyJavaTechie 3 жыл бұрын
Thanks Joseph. Please share our youtube channel with your friends and colleuges as it may helpful for them. Happy Learning.
@unconscious369
@unconscious369 2 жыл бұрын
Good explanation
@macherladevasundaram4928
@macherladevasundaram4928 3 жыл бұрын
Well. Explained bro.. But bit confused😕..
@raghupathibanoth4622
@raghupathibanoth4622 4 жыл бұрын
Super explanation...I love you bro...
@SivaReddyJavaTechie
@SivaReddyJavaTechie 4 жыл бұрын
Thanks Raghu. Please refer our KZbin channel to your friends circle as it might be helpful for them. Happy learning.
@shafivullar4180
@shafivullar4180 7 жыл бұрын
Explanation is simple and so nice
@prabashyd4504
@prabashyd4504 5 жыл бұрын
superb Explanation bro.
@SivaReddyJavaTechie
@SivaReddyJavaTechie 5 жыл бұрын
Thanks Prabas. Please refer our KZbin channel to your friends as it might be helpful for them. Happy Learning.
@viplovedev
@viplovedev 6 жыл бұрын
Clearly and lucidly explained
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Thanks Viplove
@codewithneeraj93
@codewithneeraj93 6 жыл бұрын
Thanks
@rahulsrivastava2578
@rahulsrivastava2578 7 жыл бұрын
After size is increased the what how does the values are stored with new size as suppose i had 4 value on index 2 and now my map is increased to 32 capacity then whether any value from 4 will be distributed to some other bucket and if yes then how
@johnnybatafljeska6368
@johnnybatafljeska6368 6 жыл бұрын
THANK YOU SIR!
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Thanks Johnny
@Rameshm-zl2vy
@Rameshm-zl2vy 6 жыл бұрын
thankyou sir
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Thanks Ramesh.
@Ravikumar-gj6qw
@Ravikumar-gj6qw 6 жыл бұрын
Y sir hash code of two keys are same in the figure in which case they will come
@utkirsoporboev5009
@utkirsoporboev5009 6 жыл бұрын
Hi Siva, thank you for this video and could you tell me how to increase table[] size when it is full after 16 element put different hashcode, thanks.
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Rehashing increases the number of available buckets as a function of the number of entries currently stored in the HashMap.It occurs when the HashMap implementation determines the number of buckets should be increased in order to maintain the expected O(1) lookup and insertion performance.You are correct regarding the .75 default load factor and how it will cause the HashMap to be rehashed when the 13th entry is added.However, it is not correct that default capacity of HashMap 16 means it can store 16 element in it. Any bucket can store multiple entries. However, in order to preserve the desired performance, the average number of entries per bucket should be small. That's the reason we have the load factor, and the reason we should use a proper hashCode() that spreads the keys as evenly as possible across the buckets.
@kumaragniveshverma8134
@kumaragniveshverma8134 7 жыл бұрын
All content are appreciable, except hashCode and equal method implementation u r looking confused on both the method.
@prashanthponnuswamy8101
@prashanthponnuswamy8101 6 жыл бұрын
Sir how does treeset work...
@deepak123417
@deepak123417 7 жыл бұрын
Still load factor presence is not clear if load factor is not present and resizing will happening when we put 17th element, whats is the harm in it....
@satishrock9621
@satishrock9621 6 жыл бұрын
Load factor will be calculated when HashMap reaches it's 3/4 capacity. For example, initial capacity is 16 and load factor is 0.75 nothing but 3/4, so 16X(3/4) = 12 means when 12th element is inserted then size will be doubled.
@ManpreetSingh-ei3gc
@ManpreetSingh-ei3gc 7 жыл бұрын
Hi, null value is stored in bucket 0 , that is OK. What if the first value is not null then in which bucket it will get stored?
@B-Billy
@B-Billy 7 жыл бұрын
according to calculated HASH CODE.
@ibrahimshaikh3642
@ibrahimshaikh3642 5 жыл бұрын
Nice sir
@SivaReddyJavaTechie
@SivaReddyJavaTechie 5 жыл бұрын
Thanks Ibrahim. Please refer our KZbin channel to your friends circle as it might be helpful for them. Happy Learning.
@ibrahimshaikh3642
@ibrahimshaikh3642 5 жыл бұрын
@@SivaReddyJavaTechie sure sir,plz make same video for LinkedHashMap and TreeMap
@SivaReddyJavaTechie
@SivaReddyJavaTechie 5 жыл бұрын
@@ibrahimshaikh3642 .sure. will upload video on requested topic soon.
@ibrahimshaikh3642
@ibrahimshaikh3642 5 жыл бұрын
@@SivaReddyJavaTechie hi sir, We are still waiting for LinkedHashMap and TreeMap implementation for the same, no one has created video like this before
@SivaReddyJavaTechie
@SivaReddyJavaTechie 5 жыл бұрын
@@ibrahimshaikh3642 Mostly by end of this week, I will upload video on TreeMap internal implementation.
@ravikrishnas7665
@ravikrishnas7665 7 жыл бұрын
Hi Siva Reddy. Thank you, Clear explanation. One Question. As HashMap allows only one null key, in the 0th element only one value will be inserted, but as per the diagram there are two values inserted with null key. please reply on this. Thanks in Advance
@SivaReddyJavaTechie
@SivaReddyJavaTechie 7 жыл бұрын
Hi Ravi, If you insert a null key then only it will inserted at 0th element. In the diagram, the concept is when more than one key having the same hashcode then for the particular bucket(index postion), hashMap stores in the form of Entry(key-value) which internally uses LinkedList. So In the diagram, at the bucket 0th position more than one key having the same hashcode which results into both keys will represent in the same bucket and entriesl stored in the form of LinkedList. Hence same bucket index pointing to more than one key-value pair. Thanks, SivaReddy
@jaganr5725
@jaganr5725 7 жыл бұрын
Hai, not only null value stores in 0 position but also hash code key calculated values also comes with 0 index.like hashcode is 37520 and table size is 16 .in this scenario hascode index 0 comes has zero.
@kishoreramana1
@kishoreramana1 7 жыл бұрын
can you please explain, how 2 or more values(V) having the same hashcode() and different equals() logic, will store in a same bucket (same index)?
@ganeshk3920
@ganeshk3920 6 жыл бұрын
Hi Ravi, I have one query 1.why hash map accept the null as key 2.why hash table does not accept the null as key
@senthilkumar-ki7fk
@senthilkumar-ki7fk 5 жыл бұрын
pls share the source code used in this lecture... thanks in advance
@shafivullar4180
@shafivullar4180 7 жыл бұрын
When to use abstract class and interface in java.
@SivaReddyJavaTechie
@SivaReddyJavaTechie 7 жыл бұрын
Use an abstract class when you want to define a template for a group of sub-classes , and you have at least some implementation code that call sub-classes could use.Use an interface when you want to define the role that other classes can play, regardless of where those classes are in the inheritance relation.
@shivanaikrachanaikar2843
@shivanaikrachanaikar2843 7 жыл бұрын
can you please explain with example for the above explanation.
@ggova1769
@ggova1769 7 жыл бұрын
what is bucket how it works
@SivaReddyJavaTechie
@SivaReddyJavaTechie 7 жыл бұрын
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table. Each bucket can have multiple key-value pairs. The object's hashCode() determines which bucket it goes into, via this expression: object.hashCode() % n, where n = the total number of buckets and % is the modulus operator. Most often the objects will be well distributed across buckets, but you have no guarantee where they go. This depends on the data and the hashCode function. Obviously, when the hashCode implementation is poor, the performance of the hashmap will go down. Also read up on the equals / hashcode contract, which is relevant
@shivasankar248
@shivasankar248 7 жыл бұрын
Hi SivaReddy, Thanks for providing us very detailed explanation. I have one doubt, what happens at the time rehashing ? Is there any relation in hashcode() generation and capacity of bucket. I mean, If i create hashmap with initial capacity with 100. Is there any difference in calculating hash ? Thanks in advance !!
@SivaReddyJavaTechie
@SivaReddyJavaTechie 7 жыл бұрын
Rehashing can be done in two cases: 1) When the present m'/n ratio increases beyond the load factor 2) M'/n ratio falls to a very low value say 0.1 In both the cases m' is the current number of entries. Also, both the cases demand the shifting of the present entries into a bigger or a smaller hash table. In the question's context rehashing is the process of applying a hash function to the entries to move them to another hash table. It is possible to use the hash function which was used earlier or use a new function altogether. Rehashing is also done when a collision occurs.
@satishrock9621
@satishrock9621 6 жыл бұрын
There is no concept of capacity of a bucket at all. The capacity defines the number of buckets
@durgasinghthakur
@durgasinghthakur 6 жыл бұрын
hashmap will give only wrong key when you are you using custom(Employee) object as key because when u use string or wrapper class they have implemented hash code ,equals ,to string method they are calculation are in cache n they are faster kindly give custom object implementation thanks in advance
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Hi Durga, Please go through this video @ kzbin.info/www/bejne/fp3TiKV-aKiWpa8
@MayankGupta-el9rj
@MayankGupta-el9rj 7 жыл бұрын
Hi Siva Reddy. Thank you for nice explanation.
@SivaReddyJavaTechie
@SivaReddyJavaTechie 7 жыл бұрын
Thanks Mayank.
@ashwanipandey9752
@ashwanipandey9752 6 жыл бұрын
Confusing
@Ravikumar-gj6qw
@Ravikumar-gj6qw 6 жыл бұрын
Sir pl reply me
@akashphadtare311
@akashphadtare311 6 жыл бұрын
Good explanation about internal working of hash map
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Thanks Akash.
@erpandey9011
@erpandey9011 7 жыл бұрын
Good explanation about hash map
@SivaReddyJavaTechie
@SivaReddyJavaTechie 7 жыл бұрын
Thanks Vibhuti.
@YogeshDarji99
@YogeshDarji99 6 жыл бұрын
Thanks for clear explanation. Can hashMap take 'Object' as a key? If yes, how do we retreive objects value from map?
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
Yes, An Object can be passed as Key in HashMap. If you want to do so, you need to override equals and hashcode methods in the class to make sure that if two objects are equals using equals method, for those two objects , it should return same hashcode.
@SivaReddyJavaTechie
@SivaReddyJavaTechie 6 жыл бұрын
I uploaded a video to explain this concept : kzbin.info/www/bejne/fp3TiKV-aKiWpa8
@YogeshDarji99
@YogeshDarji99 6 жыл бұрын
Thanks for creating a video to explain this concept.
@satishrock9621
@satishrock9621 6 жыл бұрын
You can you object type, not Object. You have to override hashCode() and equals methods to remove duplicates
@Ravikumar-gj6qw
@Ravikumar-gj6qw 6 жыл бұрын
Explanation is not good sir try again nxt time
How to use User defined Object as key in HashMap with an example
15:39
KINDNESS ALWAYS COME BACK
00:59
dednahype
Рет қаралды 128 МЛН
Vivaan  Tanya once again pranked Papa 🤣😇🤣
00:10
seema lamba
Рет қаралды 33 МЛН
Java Interview Questions and Answers on HashMap
16:47
Siva Reddy
Рет қаралды 21 М.
When to use Abstract class and When to use Interface in Java
20:04
About java.lang.String, why String is special in java - String tutorial
39:19
Ranjith ramachandran
Рет қаралды 108 М.
How HashMap works in Java? With Animation!! whats new in java8 tutorial
15:29
Ranjith ramachandran
Рет қаралды 1 МЛН
Top 25 Microservice Interview Questions Answered - Java Brains
39:54
KINDNESS ALWAYS COME BACK
00:59
dednahype
Рет қаралды 128 МЛН