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

Поділитися
Вставка
  • Опубліковано 9 січ 2025

КОМЕНТАРІ • 76

  • @aleksandrefimov8754
    @aleksandrefimov8754 Рік тому +2

    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)."

  • @rishibharadwaj68
    @rishibharadwaj68 3 роки тому +10

    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 3 роки тому +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 3 роки тому +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 роки тому +1

    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);
    }
    }

  • @arvindhr.a2315
    @arvindhr.a2315 3 роки тому +10

    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 :)

  • @ramyagunaseelan9435
    @ramyagunaseelan9435 3 роки тому +4

    If same hashcode here for different keys then? what about collision and threshold applies here.....?
    naveen pls reply

  • @SarangHoley
    @SarangHoley 4 роки тому +1

    Back to back new video 👍😊, thanks for the video Naveen

  • @dikshabaluja
    @dikshabaluja 3 роки тому +1

    You explain really well, Thank you so much

  • @Vishalfootball
    @Vishalfootball 4 роки тому +2

    nice one Naveen, was waiting for this

  • @manikandan2890
    @manikandan2890 3 роки тому +1

    what an amazing explanation..Thank you sir

  • @mohammedadnan984
    @mohammedadnan984 2 роки тому +1

    Best explanation 🔥

  • @user-fl5iv6nr2k
    @user-fl5iv6nr2k Рік тому +1

    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?

  • @socialmedia434
    @socialmedia434 4 роки тому +5

    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 роки тому +1

      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);
      }
      }

  • @nehasinghal3785
    @nehasinghal3785 4 роки тому +11

    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 роки тому +3

      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 😇

  • @chetanpalekar376
    @chetanpalekar376 3 роки тому +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 роки тому +1

      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?

  • @SiyaliShiya
    @SiyaliShiya Рік тому +1

    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 роки тому +2

    how collisions are handled in this?

  • @anshubajaj2842
    @anshubajaj2842 Місяць тому

    Hi Nabin, I believe the explanation is correct but its important to have a next pointer as well in the node structure of the linkedlist to have multiple nodes for a single index in case of collisions. The major difference of having before and after pointers and a next pointer for a collision scenario should have been a better explanation. Please let me know in case you have any other thoughts. Thanks for the content. - Anshu Bajaj

  • @mollamainuddin6485
    @mollamainuddin6485 3 роки тому +2

    If in case of LinkedHashMap collisions is occured then what will happen?

  • @uatul25
    @uatul25 4 роки тому +1

    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?

  • @sureshgarine
    @sureshgarine 4 роки тому +1

    Nice explanation Naveen! do we have any methods to retrieve the previous node from a particular node since it's implementing doubly-linked lists?

  • @nirmalabiradar2704
    @nirmalabiradar2704 4 роки тому +1

    Thank you Naveen 👍

  • @mrudulaahire1360
    @mrudulaahire1360 Рік тому

    What will be space complexity here for linkedhashmap?

  • @TheRajatsaluja
    @TheRajatsaluja 3 роки тому

    Naveen can you share the collision in case of LinkedHashMap

  • @RajkumarVishwakarma
    @RajkumarVishwakarma Рік тому

    Sir what happens if index for 2 different keys is same.. Then how the two key value pairs will be stored?

  • @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. ?

  • @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.

  • @superbbcfan
    @superbbcfan 3 роки тому +1

    Hi Naveen ! Can collision occur in LinkedHashMap? If so, what happens in that case?

  • @Shreenidhi110
    @Shreenidhi110 10 місяців тому

    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

  • @manichowdry547
    @manichowdry547 4 місяці тому

    EXCELLENT

  • @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??

  • @amolchaure6309
    @amolchaure6309 2 роки тому

    what happened if we get same index to with different key to store into linked list then how it manage internally?

  • @kumarjadhav6106
    @kumarjadhav6106 2 роки тому

    is there any video for internal working of LinkedHashSet

  • @atifpall2007
    @atifpall2007 4 роки тому

    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 4 роки тому

      Does it change to a balanced binary tree once it reaches the threshold 8 nodes?

  • @ankitmahajan8295
    @ankitmahajan8295 4 роки тому +1

    to the point thank u sir ji

  • @sahuamit30
    @sahuamit30 3 роки тому

    how collision will be handled in case of linkedhashmap ?

  • @kanishkasrivastava9108
    @kanishkasrivastava9108 4 роки тому

    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.

  • @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.

  • @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 Рік тому

      It will be the same, an additinal node will be created and doubly linked list pointers will be appropriately updated.

  • @dasarimanasvi128
    @dasarimanasvi128 3 роки тому

    i have one doubt , how exactly its start from 4th index when we executing the entire LHP,

  • @rahulsuthar319
    @rahulsuthar319 Рік тому

    what in case of hash collision ?

  • @kishorekisu6357
    @kishorekisu6357 3 роки тому

    in linkedhashmap .. what happens when multiple values are stored in same index

  • @rajatroy100
    @rajatroy100 3 роки тому

    How collisions are handled in linkedhashmap?

  • @sovanghosh2524
    @sovanghosh2524 2 роки тому

    what about if a collision occurs?

  • @glennmaxi7542
    @glennmaxi7542 3 роки тому

    what is hash collision happened in linkedhashmap

  • @prernach417
    @prernach417 2 роки тому

    add collision case well.

  • @blaxminarayanpatra7311
    @blaxminarayanpatra7311 3 роки тому

    if same index no then how it will store ??

  • @ashishupadhyay8360
    @ashishupadhyay8360 3 роки тому

    What will happen in case of collision ? Many people commented this question. We all want reply. Please reply.

  • @suminshakya5067
    @suminshakya5067 4 роки тому

    What about hash collision

  • @sssrinivas1
    @sssrinivas1 4 роки тому

    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()){
      }
      }

  • @JeetenGandhi
    @JeetenGandhi 4 роки тому

    Can someone share telegram channel link?

    • @ai_pni
      @ai_pni 4 роки тому

      t.me/joinchat/COJqZUPB02pj9vVmu8Yv7Q

  • @akashingle3404
    @akashingle3404 Рік тому

    Nice

  • @godoffortuneful
    @godoffortuneful 4 роки тому +2

    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 4 роки тому

    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  4 роки тому +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  4 роки тому +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 3 роки тому +1

      @@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.