#14 - linkedhashmap vs hashmap in Java || How LinkedHashMap works internally - Naveen AutomationLabs

  Рет қаралды 40,856

Naveen AutomationLabs

Naveen AutomationLabs

Күн бұрын

Пікірлер: 74
@arvindhr.a2315
@arvindhr.a2315 3 жыл бұрын
Great Video Naveen sir ! Can you add some comments or a short video for collision in linked hashmap ? I see many folks have requested for the same . if you could, it would be great :)
@rishibharadwaj68
@rishibharadwaj68 3 жыл бұрын
Good to see a video on the internal implementation of a Collection class! But, I have an interesting question here : What if 2 keys have the same hashCode? As per your logic, they will generate same index. How will you store 2 nodes under the same segment(bucket)? I think instead of 2 pointers the nodes must have 3 pointers. 1. BackwardNode 2. ForwardNode 3. NextNode to point to a Node belonging to the same index(bucket). Not sure if LinkedHashSet is implemented this way. Would like to hear from you.
@ayushraj-zb6sv
@ayushraj-zb6sv 3 жыл бұрын
yaeh, even i have same doubt
@BitExplorer
@BitExplorer 2 жыл бұрын
True that we will have 3 pointers in total which are [before, after] to maintain insertion order and [next] pointer to manage collision scenarios.
@NehaSingh-um5ib
@NehaSingh-um5ib 2 жыл бұрын
suppose we have 2 keys with same hashcode key1 and key2 so ....key 1's after node will connect to keys 2's before node and key 2's after node will combine to key of another segment
@hardiknegi435
@hardiknegi435 2 жыл бұрын
Will collision not be handled like it is handled in normal Maps? like a new Node at same index will be created with its Before Pointer pointing to already present node's After Pointer and new node's Next pointer will point to previous node's after pointer
@thanga2317
@thanga2317 3 жыл бұрын
Thanks for the explanation Naveen. You should've explained about Entry which is wrapper of Node. HashMap.Node subclass for normal LinkedHashMap entries. static class Entry extends HashMap.Node { Entry before, after; Entry(int hash, K key, V value, Node next) { super(hash, key, value, next); } }
@nehasinghal3785
@nehasinghal3785 3 жыл бұрын
How the collision is handled in LinkedHashMap?
@imdadkhan5047
@imdadkhan5047 3 жыл бұрын
Neha in case of collision in linkedhashmap it will simply create one single linkedlist and once the threshold value is reached it will be converted into a balanced binary tree
@mohdrasid6737
@mohdrasid6737 2 жыл бұрын
There will be one more link of name "next", which will store the address of next node of the same bucket(segment). There will be total of 6 fields as below- before - store the address of the node which was just created before this new node node. key - key of the (key - value) pair hash - hash code of the key value - value of (key - value) pair after - store the address of the node which is just created after this node. next - store the address of the next node of the same bucket(segment). so , before and after - are used to maintain insertion order . but "next" is used to main all nodes of the same bucket (segment). Thanks 😇
@user-fl5iv6nr2k
@user-fl5iv6nr2k 9 ай бұрын
Hello @Naveen, I have one question regarding linkedHashMap, as per your explanation, does this mean 1 segment will have only one node created at any given point unlike HashMap where we could have maximum 8 nodes per segment?
@ramyagunaseelan9435
@ramyagunaseelan9435 3 жыл бұрын
If same hashcode here for different keys then? what about collision and threshold applies here.....? naveen pls reply
@SiyaliShiya
@SiyaliShiya Жыл бұрын
Hi, Thank you for the explanation, but I have a question when a collision occurs how the behavior be? because we are already pointing before and after node?
@bharathreddy8918
@bharathreddy8918 3 жыл бұрын
how collisions are handled in this?
@aleksandrefimov8754
@aleksandrefimov8754 9 ай бұрын
Just copied info from the most helpful comments below about the case of collision in LinkedHashMap. Hope it will help you) "In case of collision in linkedhashmap it will simply create one single linkedlist and once the threshold value is reached it will be converted into a balanced binary tree" - the same principle as in HashMap "There will be one more link of name "next", which will store the address of next node of the same bucket(segment). There will be total of 6 fields as below- -before - store the address of the node which was just created before this new node node. -key - key of the (key - value) pair -hash - hash code of the key -value - value of (key - value) pair -after - store the address of the node which is just created after this node. -next - store the address of the next node of the same bucket(segment). so , before and after - are used to maintain insertion order . but "next" is used to main all nodes of the same bucket (segment)."
@dikshabaluja
@dikshabaluja 3 жыл бұрын
You explain really well, Thank you so much
@mollamainuddin6485
@mollamainuddin6485 3 жыл бұрын
If in case of LinkedHashMap collisions is occured then what will happen?
@socialmedia434
@socialmedia434 3 жыл бұрын
Hi Naveen, can collision occur in the LinkedHasMap also. In that case what happens to the before and after nodes?
@uigreseu5756
@uigreseu5756 3 жыл бұрын
I believe that a nod also has and a link which connects two nodes in the same index. in case that in an index are two nodes then this link also will connect them. Hope this the answer to your question.
@thanga2317
@thanga2317 3 жыл бұрын
He missed those points: HashMap.Node subclass for normal LinkedHashMap entries. static class Entry extends HashMap.Node { Entry before, after; Entry(int hash, K key, V value, Node next) { super(hash, key, value, next); } }
@manikandan2890
@manikandan2890 3 жыл бұрын
what an amazing explanation..Thank you sir
@SarangHoley
@SarangHoley 3 жыл бұрын
Back to back new video 👍😊, thanks for the video Naveen
@chetanpalekar376
@chetanpalekar376 3 жыл бұрын
Hi Naveen, Thank you for this explanation. Just a quick query, what happens here, if the segment index of the second value I am inserting is calculated to be the same as the index of the first value? In your HashMap implementation video example you have had mentioned that the segment index of different values can be the same. Is is not the same for LinkedHashMap? Thanks!!
@moonshine4757
@moonshine4757 2 жыл бұрын
I've same doubt. If index is based on how we insert, what's the use of hash code? Also what about Collison. Did you clear your doubt?
@Vishalfootball
@Vishalfootball 3 жыл бұрын
nice one Naveen, was waiting for this
@nirmalabiradar2704
@nirmalabiradar2704 3 жыл бұрын
Thank you Naveen 👍
@Shreenidhi110
@Shreenidhi110 7 ай бұрын
I have one doubt: what if the aftr calulating index value for 1st entry is 3 and 2nd entry its 1 ,then what will be the insertion order?I am confused here
@sureshgarine
@sureshgarine 3 жыл бұрын
Nice explanation Naveen! do we have any methods to retrieve the previous node from a particular node since it's implementing doubly-linked lists?
@mohammedadnan984
@mohammedadnan984 2 жыл бұрын
Best explanation 🔥
@dineshkumarpanda7287
@dineshkumarpanda7287 2 жыл бұрын
I have one doubt, could you please explain- why hashmap doesn't maintain order and why linkedhashmap maintains order. As both uses linkedlist to store elements.
@manichowdry547
@manichowdry547 Ай бұрын
EXCELLENT
@ankitmahajan8295
@ankitmahajan8295 3 жыл бұрын
to the point thank u sir ji
@mrudulaahire1360
@mrudulaahire1360 Жыл бұрын
What will be space complexity here for linkedhashmap?
@xoda345
@xoda345 2 жыл бұрын
what is the purpose of calculating index in linkedhashmap bcause all the nodes are divided in 4 partition which contains - before address, key,value and after address. Where do the index or the hashcode gets stored? So how would the JVM know which index is assigned to which node??
@MumPuneMum
@MumPuneMum 3 жыл бұрын
Thanks boss for such explanation. 1 quick doubt - what if the hashcode are same ? like if we get 1 more similar hashcode at index 4th .. how it handles the before and after node of previous segment?
@nandhinimathivanan3998
@nandhinimathivanan3998 3 жыл бұрын
i also have the same doubt Naveen please help us on this
@sujeetkumar-dt7xk
@sujeetkumar-dt7xk 3 жыл бұрын
I am also having same doubt please tell if anyone knows
@kumarjadhav6106
@kumarjadhav6106 2 жыл бұрын
is there any video for internal working of LinkedHashSet
@moonshine4757
@moonshine4757 2 жыл бұрын
Since index is decided is based on hash code, how is it ordered collection ? What if some key get index of 2 (which is inserted in the end) & what about Collison? Doesn't it happen here?? I understood o/p will be just like i/p tho.
@RajkumarVishwakarma
@RajkumarVishwakarma Жыл бұрын
Sir what happens if index for 2 different keys is same.. Then how the two key value pairs will be stored?
@superbbcfan
@superbbcfan 3 жыл бұрын
Hi Naveen ! Can collision occur in LinkedHashMap? If so, what happens in that case?
@kishorekisu6357
@kishorekisu6357 3 жыл бұрын
same doubt!!!!!
@sheshankgolli2667
@sheshankgolli2667 3 жыл бұрын
While we are inserting next elements, if we get index is lesser than the previous node, how they will connect before and after and maintains the insertion order. ?
@kanishkasrivastava9108
@kanishkasrivastava9108 3 жыл бұрын
and what will happen when collision happens. Even in HashMap values are stored in the same way. What's the difference between HashMap and LinkedHashMap.
@TheRajatsaluja
@TheRajatsaluja 3 жыл бұрын
Naveen can you share the collision in case of LinkedHashMap
@dasarimanasvi128
@dasarimanasvi128 3 жыл бұрын
i have one doubt , how exactly its start from 4th index when we executing the entire LHP,
@kishorekisu6357
@kishorekisu6357 3 жыл бұрын
in linkedhashmap .. what happens when multiple values are stored in same index
@ashishupadhyay8360
@ashishupadhyay8360 3 жыл бұрын
What will happen in case of collision ? Many people commented this question. We all want reply. Please reply.
@atifpall2007
@atifpall2007 3 жыл бұрын
Naveen sir how do we handle collision concept here like we discussed in hashMap videos. How the nodes will be created if the indexes are same ? How the get() will work for duplicate indexes? Thank you 🙏
@atifpall2007
@atifpall2007 3 жыл бұрын
Does it change to a balanced binary tree once it reaches the threshold 8 nodes?
@sahuamit30
@sahuamit30 3 жыл бұрын
how collision will be handled in case of linkedhashmap ?
@sovanghosh2524
@sovanghosh2524 2 жыл бұрын
what about if a collision occurs?
@rahulsuthar319
@rahulsuthar319 Жыл бұрын
what in case of hash collision ?
@amolchaure6309
@amolchaure6309 2 жыл бұрын
what happened if we get same index to with different key to store into linked list then how it manage internally?
@prernach417
@prernach417 Жыл бұрын
add collision case well.
@rajatroy100
@rajatroy100 2 жыл бұрын
How collisions are handled in linkedhashmap?
@blaxminarayanpatra7311
@blaxminarayanpatra7311 3 жыл бұрын
if same index no then how it will store ??
@akashingle3404
@akashingle3404 Жыл бұрын
Nice
@sssrinivas1
@sssrinivas1 3 жыл бұрын
How can we iterate a map which is inside a map? Like HashMap map= new HashMap();
@emilyding4676
@emilyding4676 3 жыл бұрын
iterator in java
@karwannouri8266
@karwannouri8266 2 жыл бұрын
for( Map.Entry outerEntry: map.entrySet()){ for(Map.Entry innerEntry : outerEntry.getValue().entrySet()){ } }
@glennmaxi7542
@glennmaxi7542 3 жыл бұрын
what is hash collision happened in linkedhashmap
@venkatramanhiregange9096
@venkatramanhiregange9096 3 жыл бұрын
What happens if we get same hashcode for the keys?(like in in hash map it maintains the linked list in case hashcode is same)
@profitableloser3879
@profitableloser3879 10 ай бұрын
It will be the same, an additinal node will be created and doubly linked list pointers will be appropriately updated.
@suminshakya5067
@suminshakya5067 3 жыл бұрын
What about hash collision
@JeetenGandhi
@JeetenGandhi 3 жыл бұрын
Can someone share telegram channel link?
@ai_pni
@ai_pni 3 жыл бұрын
t.me/joinchat/COJqZUPB02pj9vVmu8Yv7Q
@godoffortuneful
@godoffortuneful 3 жыл бұрын
First view n comment
@berkanyildiz5343
@berkanyildiz5343 2 жыл бұрын
I think you dreamed of becoming a doctor, but unfortunately, life forced you to become a developer. Dude, what is this writing style?
@abhineetgarg2045
@abhineetgarg2045 3 жыл бұрын
why is it required to maintain the previous node when you are not retrieving it? And what if there will be a collision then you will lost the hash concept too.. it will work like linked arraylist if collision starts. Totally disagree with the concept
@naveenautomationlabs
@naveenautomationlabs 3 жыл бұрын
Your understanding is wrong about this concept. And why do u think in case of collision you will be loosing hash concept? And first of all its not linekdArraylist (its not even exist), it's a doubly Linkedlist. FYI, this is the internal implementation of LinkedHashMap in java library, so you are not agree with Java or with the concept? And If you don't agree, please explain why r u not agree with this concept? And what solution you propose to maintain an insertion order for maps?
@naveenautomationlabs
@naveenautomationlabs 3 жыл бұрын
And you need to maintain the previous node so that you can maintain the insertion order. That's the property of LinkedHashMap as compared to hashmap.
@BitExplorer
@BitExplorer 2 жыл бұрын
@@naveenautomationlabs , True that we will have 3 pointers in total which are [before, after] to maintain insertion order and [next] pointer to manage collision scenarios.
#15 - Top 20 HashMap Interview Questions in Java || By Naveen AutomationLabs
27:35
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 12 МЛН
LinkedHashMap and LinkedHashSet in Java | Internal Working
16:58
Daily Code Buffer
Рет қаралды 21 М.
Why String is popular HashMap key in Java?
16:53
Naveen AutomationLabs
Рет қаралды 15 М.
Map and HashMap in Java with Internal Working- Interview Question
19:27
Daily Code Buffer
Рет қаралды 30 М.
HashMap, LinkedHashMap and TreeMap in Java
15:51
Easy Learning
Рет қаралды 27 М.
01. Internal Working of HashMap & Java-8 Enhancement
19:11
Ankit Wasankar
Рет қаралды 115 М.
Последствия выхода Айфона 16
0:23
ТРЕНДИ ШОРТС
Рет қаралды 4,3 МЛН
iPhone or Samsung?
0:28
Kan Andrey
Рет қаралды 1,5 МЛН
iPhone Standby mode dock, designed with @overwerk
0:27
Scott Yu-Jan
Рет қаралды 6 МЛН
😍Самый ПРИЯТНЫЙ Айфон🔥
0:34
Demin's Lounge
Рет қаралды 679 М.
Почему нужно включать режим самолета 😰
0:39