How HashMap works internally || Popular java interview question on collection (HashMap)

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2018
  • In this tutorial, we are going to learn how hashmap works internally.This is a very popular java interview question from the collection framework and been asked so many times to check candidates understanding on Map collection.
    I am going to cover three things in this series of tutorials which will cover a hashmap internal implementation.
    1) We will see a detailed overview of how a map works internally when we use map.put(Key k, Value v )
    2)Once we will have an overall idea we are going to debug
    everything that we learned in this tutorial
    3)In the last tutorial of this series, I will cover how the get method works internally ( map.get(Key K) )
    So when we create a map object internally an array of buckets will be created with a size 16 (if we use the default constructor.)Then when we use the map.put method, internally the hashcode and the index will be calculated for the key and the key will be stored in a specific index.
    In case of a hash collision, then the key with the same hashcode is going to be stored in the same index as each bucket is a node or a linked list.
    Inside each bucket, four things are going to be stored as a form of a linked list.They are hashcode, key, value and the next (which hold the address of the next node with the same hashcode).
    So lets understand everything diagrammatically because this is one of the very popular hashmap interview questions.So please put this question on your to-do list and better be prepared for it.
    “How hashmap works internally in java with example"
    subscribe to my channel by clicking on the link below.
    / @seleniumexpress
    Stay tuned and like my Facebook page for more.
    / seleniumexpress
    Music :
    -----------
    credits : -
    1)
    Adventures by A Himitsu / a-himitsu
    Creative Commons - Attribution 3.0 Unported- CC BY 3.0
    creativecommons.org/licenses/b...
    Music released by Argofox • A Himitsu - Adventures...
    Music provided by Audio Library • Adventures - A Himitsu...
    (intro)
    2)
    NCM Epic Music Ender Guney
    / ncmepicmusic
    -----
    intro template :
    / alexbau01

ความคิดเห็น • 418

  • @naturelove1525
    @naturelove1525 4 ปีที่แล้ว +67

    If I'm not wrong ,it will not create another linked list ..it will just create new node and will make it was next

    • @SeleniumExpress
      @SeleniumExpress  4 ปีที่แล้ว +28

      That is correct, I said that by mistake.Thank you for correcting me.😊
      Please watch the next video of the playlist.I have given a complete code walkthrough.
      th-cam.com/video/fSjxhOYPBRI/w-d-xo.html

    • @naturelove1525
      @naturelove1525 4 ปีที่แล้ว +1

      @@SeleniumExpress thank you ...

    • @gayatriyeole1795
      @gayatriyeole1795 3 ปีที่แล้ว +5

      Very nice explanation... Can u plz tell me how to calculate hashcode and index properly. Becoz u radomly told 1111112.Internally how it is calculated plz tell.

    • @sagarkorde209
      @sagarkorde209 3 ปีที่แล้ว +4

      @@gayatriyeole1795 durga sir ko refer karo

    • @honeyverma6549
      @honeyverma6549 3 ปีที่แล้ว +7

      durga sir ki kaafi purani video h ab version bdl gy h java ke but Durga sir is like god of java

  • @BuffaloCanCode
    @BuffaloCanCode 6 ปีที่แล้ว +27

    2 Things I want to add (correct) here
    1. When you say a link-list will be added, actually it would be a node added into a particular linkedlist.
    2. map.put("null", 4) is not equal to map.put(null, 4). In case of map.put("null", 4) hashcode will be calculated as normal String. What you explained for 0th index is map.put(null, 4).

  • @meghatiwari5225
    @meghatiwari5225 3 ปีที่แล้ว +8

    Thank you so much for very good way explained this topic with logic. It’s really helpful for anyone who wants to learn java. Thanks again.

  • @ashrithareddyrondla9882
    @ashrithareddyrondla9882 3 ปีที่แล้ว +7

    Preparing for interviews and found this very useful thanks a ton!!!!!!!!!😁👍🙏

  • @ShadowWalker683
    @ShadowWalker683 3 ปีที่แล้ว

    You have no idea how this video helped me. I have seen a bunch of other videos and the tutorials just make the concept much more complex. Thanks a bunch man! Keep up the good work.

  • @prafulsurve3190
    @prafulsurve3190 6 ปีที่แล้ว +6

    Nice video. In case of a collision if we add the new node to the beginning of the LinkedList in the bucket then we can still achieve O(1) time complexity for put() method instead of O(n). The much elegant solution to improve the hashmap collision performance would be to use self-balancing tree data structure so in the worst case traversal would be O(log n).

    • @irynasherepot9882
      @irynasherepot9882 4 ปีที่แล้ว

      Yeah, I think Java implementation updated it as well

  • @narendarb7308
    @narendarb7308 4 ปีที่แล้ว

    Wow, it's amazing experience , now I understand that what exactly it is working internally..superb🙏

  • @subhojitdas1727
    @subhojitdas1727 3 ปีที่แล้ว

    Explained in a plain simple easily understandable way. Really helpful. Thank you!!

  • @rajatgoyal1491
    @rajatgoyal1491 4 ปีที่แล้ว +1

    Nice briefing over internal implementation of HashMap

  • @akware977
    @akware977 4 ปีที่แล้ว +1

    Thank you so much to clear the question deeply in simple words

  • @MicroServiceMan
    @MicroServiceMan 5 ปีที่แล้ว +40

    At end u told map.put("null",4) but it should be map.put(null,4) ... Means without double quotes.... Otherwise it will become string

  • @pujabansal810
    @pujabansal810 4 ปีที่แล้ว

    I was never able to understand this topic. After watching your videos concepts are very cleared

  • @akshayjeengar2576
    @akshayjeengar2576 3 หลายเดือนก่อน

    Watched this video yesterday before my Interview and Luckily I was asked to implement my own hashMap. My code worked in the first attempt. Kudos to you explaination.

  • @MrSubandrio
    @MrSubandrio 2 ปีที่แล้ว

    i remember that. when i was young, i make simple like a database, iam using link list, exactly iam using doubly link list .. with 'prev' and 'next' node, good job bro !.

  • @tejusree2072
    @tejusree2072 6 ปีที่แล้ว +2

    Hi.
    I am glad that found your tutotial.It was very unique that providing and explaining with debugging so that the logically can understand the point.It was very nice.But please do the titorial on Threads ,Serilization please.

  • @priyankashinde1424
    @priyankashinde1424 5 ปีที่แล้ว

    Thank you so much for explaining very well please keep it on your videos for another's concepts and practicals of java it will really help us.

  • @SonalGupta25
    @SonalGupta25 5 ปีที่แล้ว +1

    Awesome Explanation!! just loved it..

  • @pranjaliwagh4900
    @pranjaliwagh4900 5 ปีที่แล้ว +2

    Great tutorial... appreciate your work...thank you..

  • @basavarajnv1017
    @basavarajnv1017 3 ปีที่แล้ว

    Now I really understood how hashmap works internally...u r really amazing brother.great explanation.🙏🙏👍

  • @ramesh22kvr
    @ramesh22kvr 4 ปีที่แล้ว

    you are explaining very clearly with diagrams.i have not seen like this in any other channels

  • @sindhuchary
    @sindhuchary 5 ปีที่แล้ว +1

    aaa & aab are 2 different keys and hence the hash code will be different. Only the index could be the same and if so the second entry will be added to the linkedlist at that particular index (index 6 in this example). Now assume that I'm trying to put another value with the same key, aaa. Now the hashcode and the index of the current entry and that of the first entry at index 6 are the same. So the value that pass with the key aaa is overwritten. This is what happens when you try to put a value into a hashmap with an already existing key, as we all know the keys are to be unique otherwise the entry will be overwritten. Keep going! Best wishes!

  • @jayakumar6091
    @jayakumar6091 4 ปีที่แล้ว

    Simple and very straight forward 😍😘

  • @bharathan1608
    @bharathan1608 5 ปีที่แล้ว

    Awesome video.. Salute to ur effort.. Now i clearly learnt about hashmap.. Thank u very much. Make more videos bro.

  • @shaktimaan693
    @shaktimaan693 3 ปีที่แล้ว

    it was epic moment when u explained about hash collision got gussbump . thak u sir

  • @shwetasarangale1591
    @shwetasarangale1591 4 ปีที่แล้ว

    Very useful.Thanks a lot for such a wonderful and simple explanation

  • @sivaranjanis8605
    @sivaranjanis8605 6 ปีที่แล้ว

    Superb explanation, It's really very useful Thank you

  • @sowmyarajendran4829
    @sowmyarajendran4829 4 ปีที่แล้ว +4

    Thanks for the video.. Got this question in my thought works interview and got cleared

  • @kraju8760
    @kraju8760 5 ปีที่แล้ว

    Good sir.its very helpful to beginners.Thank u

  • @johnwilson3918
    @johnwilson3918 6 ปีที่แล้ว +1

    The 'hash(key) & (n - 1)' statement is a quicker equivalent to 'hash(key) % n' ; but with the caveat that the bucket size (n) is a power of 2 (ie. 2, 4, 8, 16, 32, 64, 128, 256 ....). If you convert each member of the latter set to binary and subtract one and peek at the binary bit pattern - you'll see why '&' works as 'MODULUS' in these instances.

  • @pratapk.c1277
    @pratapk.c1277 2 ปีที่แล้ว

    I was asked this question yesterday on interview and now i am watching it

  • @mdnadimarif
    @mdnadimarif 3 ปีที่แล้ว

    It's amazing way of explaining things. Big thumbs up

  • @asthagarg4614
    @asthagarg4614 3 ปีที่แล้ว +2

    Amazing video but I think you should also explain about 'rehashing' also. I have always watched this video for understanding the internal working of Hashmap but interviewer asked me about how Hashmap grows and I was unable to answer that question in detail.

  • @Rithus2020
    @Rithus2020 3 ปีที่แล้ว

    Hi You did an execellent job.Crystal clear.Thanks for the video!

  • @HardikPatel-iw7ju
    @HardikPatel-iw7ju 4 ปีที่แล้ว

    Nice video for interview point of view.

  • @marghe174.
    @marghe174. 2 ปีที่แล้ว

    Finally I understood how is working

  • @santoshkumarsahoo1103
    @santoshkumarsahoo1103 5 ปีที่แล้ว +1

    A great one.... Please make a video on Hibernate bootstraping 3.x,4.x and 5.x.... with internal explanation and with example...

  • @goonerritz93
    @goonerritz93 4 ปีที่แล้ว

    Very well explained! Thanks!

  • @prasadp6474
    @prasadp6474 6 ปีที่แล้ว

    Superb. Seriously your explanations are so good. Please publish more concepts.

  • @shivakamlikar2144
    @shivakamlikar2144 6 ปีที่แล้ว +2

    Good voice... also good explanation

  • @AnilKumar-nq5lp
    @AnilKumar-nq5lp 2 ปีที่แล้ว

    I got a new job because of this tutorial, Thanks much!!!

  • @ravindra0295
    @ravindra0295 6 ปีที่แล้ว

    wow! superb explanations! please make the video for hashCode and .equals() method

  • @satyaranjanpatra4380
    @satyaranjanpatra4380 4 ปีที่แล้ว

    Your teaching style is very excellent

  • @ajbchannel9719
    @ajbchannel9719 2 ปีที่แล้ว

    OMG this guy is very nice. he helped to understand any code of java.
    thank you!
    if you can please give as more please

  • @AnshiSohamiamazingworld
    @AnshiSohamiamazingworld 3 ปีที่แล้ว

    Very well explained for hashMap internal working🙏

  • @praveenkumarc6948
    @praveenkumarc6948 3 ปีที่แล้ว +1

    Thank You so much after so many videos I have understood how it works and Successfully Subscribed❤️❤️❤️

  • @varshachavan471
    @varshachavan471 3 ปีที่แล้ว

    Very Useful content you have provided Thank you so much for making thiss video ...my concepts cleared about hashmap

  • @ramesh22kvr
    @ramesh22kvr 4 ปีที่แล้ว

    finally found very good channel.thank you sir

  • @1976balakrishna
    @1976balakrishna 5 ปีที่แล้ว

    Good work, Thanks, keep it up

  • @ashokkhatri9005
    @ashokkhatri9005 6 ปีที่แล้ว

    thanku so much sir...fantastic..

  • @ramesh22kvr
    @ramesh22kvr 4 ปีที่แล้ว

    you are explaining very very good manner sir

  • @teodorrussu777
    @teodorrussu777 6 ปีที่แล้ว +5

    great explanation! thank you for your work!

  • @swapsure
    @swapsure 5 ปีที่แล้ว

    Explanation is apt !! Kudos !!

  • @shivananadotari3572
    @shivananadotari3572 6 ปีที่แล้ว

    Great..!!! Very Nice Explanation

  • @priyankasinghai953
    @priyankasinghai953 4 ปีที่แล้ว

    Very Nice Explaination.....Thank You very much for such nice videos.....

  • @rambattula3030
    @rambattula3030 6 ปีที่แล้ว

    Super explanation
    Thanks bro

  • @shreyasoni6223
    @shreyasoni6223 2 ปีที่แล้ว

    Awesome explanation ... Thanks a lot!!

  • @ARI-em6me
    @ARI-em6me 5 ปีที่แล้ว

    Super way of teaching

  • @prasantaroutray1981
    @prasantaroutray1981 6 ปีที่แล้ว

    its highly good explanation

  • @kurabhakalyani740
    @kurabhakalyani740 4 ปีที่แล้ว

    Your way of talking is super i love it nice explanation

  • @Kajal_Kumari_Ojha
    @Kajal_Kumari_Ojha 5 ปีที่แล้ว

    I feel in the 6th node ...linked list of 1st will store the address of the next linked list and the address of the last LinkedList will be null as u have explained. Anyways Superb Explanation.

  • @sangram11100
    @sangram11100 6 ปีที่แล้ว

    Wowww I got it bro.. Thank you

  • @dvoorganisation459
    @dvoorganisation459 ปีที่แล้ว

    Explained Really well in such a short video. Kudos! Really Prepared before giving Videos on TH-cam. I can see good confidence in your presentation. Thanks a ton!

  • @priyabiradar9299
    @priyabiradar9299 6 ปีที่แล้ว +2

    Very nice explanation

  • @tindo0038
    @tindo0038 4 ปีที่แล้ว

    Nice explanation. THanks

  • @markokafor7432
    @markokafor7432 3 ปีที่แล้ว +1

    Access to a linked list is only through the head. The second value will be placed at the head while the first value will be pushed to the new node. so your first node should contain aab and the second should be aaa. That is how insertion works in linked list. You have it the other way around

  • @asemreddy1867
    @asemreddy1867 3 ปีที่แล้ว

    Nice explanation.. Thank you ..!

  • @manva007
    @manva007 5 ปีที่แล้ว +1

    thanks for sharing this video, while you are explaining collision, 2 different keys would never have the same hashcode, but they are doing mod operation same bucket number could come, and it would result into collision , it would be worth explaining the get operation, modules operation and also load factor

    • @SeleniumExpress
      @SeleniumExpress  5 ปีที่แล้ว

      Thanks Manish pointing these things out.. I have covered all the things in the follow up tutorials that you wanted me to cover.

  • @musamammadov4984
    @musamammadov4984 2 ปีที่แล้ว

    Thank you for your effort.

  • @Sri_Gayatri_1
    @Sri_Gayatri_1 2 ปีที่แล้ว

    Your super most best trainer 😎

  • @humaali4636
    @humaali4636 2 ปีที่แล้ว

    Godly tutorial... Thanks alot sir. 👏👏👏

  • @sivag8493
    @sivag8493 6 ปีที่แล้ว +1

    Great explanation, please provide collection's with practical sessions.

    • @SeleniumExpress
      @SeleniumExpress  6 ปีที่แล้ว

      Sure, Siva. I will do it pretty soon. Thank you for posting a comment.

  • @archigoel4717
    @archigoel4717 6 ปีที่แล้ว +4

    Thanks for explaining. This is a very common interview question.

  • @ramesh22kvr
    @ramesh22kvr 4 ปีที่แล้ว

    i am following every video of yours

  • @SuruchiJaiswall
    @SuruchiJaiswall 3 ปีที่แล้ว

    Superb explanation 🙏

  • @kumar55663
    @kumar55663 3 ปีที่แล้ว

    excellent explaination ...no words needed. please do more videos like this

  • @pawankpk8301
    @pawankpk8301 6 ปีที่แล้ว

    very good explanation bro..

  • @dalbirsinghdhillon1429
    @dalbirsinghdhillon1429 2 ปีที่แล้ว

    good explanation..
    in the case of the same hashcode, a linked list is created..containing same hashcode in each node of the LL.. first of all, it's same hashcode for each node and secondly, we can calculate this hashcode using key so why store it in each node.

  • @asad_mirza
    @asad_mirza 4 ปีที่แล้ว

    Thankyou for this video

  • @siddhantjaiswal3077
    @siddhantjaiswal3077 6 ปีที่แล้ว

    Nice explanation sir😊

  • @a12aify
    @a12aify 6 ปีที่แล้ว

    very useful and informative video. Please provide this type tutorial video on Spring Security. Thanks.

    • @SeleniumExpress
      @SeleniumExpress  6 ปีที่แล้ว

      Sayantan Mukhopadhyay thanks so much

  • @anuragbharti3773
    @anuragbharti3773 3 ปีที่แล้ว

    Great explanation. I surprised who disliked the video.

  • @studymathematics3297
    @studymathematics3297 6 ปีที่แล้ว +7

    Bro superbly explained,
    You have cleared my doubts
    Please make a video on inheritance and explain all such posssible cases like
    Parent p = new child()
    Child c = new parent()
    Parent p = new parent()
    Child c = new child()

    • @SeleniumExpress
      @SeleniumExpress  6 ปีที่แล้ว

      Thanks, bhai..Glad you find it helpful.
      hope this video can help :
      th-cam.com/video/vp6Mcxc1CTM/w-d-xo.html
      have a nice time !!

    • @javabava6928
      @javabava6928 6 ปีที่แล้ว +3

      Study Mathematics Child c=new Parent(); not possible because child class reference cannot hold parent class object. It will give compile time error.

  • @itisksn
    @itisksn 3 ปีที่แล้ว

    nice explanation, love it!!

  • @evaristegalois450
    @evaristegalois450 4 ปีที่แล้ว

    Thank You Very Match!!!

  • @user-st3yr9er6o
    @user-st3yr9er6o 6 ปีที่แล้ว

    OSM Explaination Bro.......

  • @srinivasvalekar9904
    @srinivasvalekar9904 6 ปีที่แล้ว

    Amazing video Bhai!!! Thanks a lot. Also please do put some video tutorials on Concurrent Hashmaps.
    Also would you please give me the links to books that you referred?

  • @MegaSethi
    @MegaSethi 4 ปีที่แล้ว

    Great explanation!!

  • @habeebashraf6610
    @habeebashraf6610 6 ปีที่แล้ว

    Nice explanation :)

  • @brundaj5568
    @brundaj5568 4 ปีที่แล้ว

    Hi , request you to teach hibernate technology ....as the spring you thought was very helpful for me and understood clearly

  • @yogeshnarwade8813
    @yogeshnarwade8813 5 ปีที่แล้ว

    Very good explanation

  • @pratapsingh-le2br
    @pratapsingh-le2br 6 ปีที่แล้ว +3

    Hii amazing explaining methodology bro will please upload the all enhancement with internal working procedure bdw already watching ur all the video related to the hash map

  • @SanjeevSingh-bo7em
    @SanjeevSingh-bo7em 3 ปีที่แล้ว

    doing great work keep doing it.

  • @manideepkumar959
    @manideepkumar959 4 ปีที่แล้ว +1

    Bro ,I think for put operation ,internally equals method also being called(since it is overriding the value for duplicate keys) can u explain that as well, it will be helpful

  • @sudhirpadhy2492
    @sudhirpadhy2492 2 ปีที่แล้ว

    Nice one !! Thanks Abhilash ..

  • @aniketbudbadkar1801
    @aniketbudbadkar1801 ปีที่แล้ว

    Gr8 explained keep it up and also share java8 new features related videos

  • @deviprasadpatnaik6837
    @deviprasadpatnaik6837 5 ปีที่แล้ว

    Awesome explanation abhilash bhai

  • @digvijaynegi6917
    @digvijaynegi6917 5 ปีที่แล้ว

    Thanks brother... 😊

  • @openarenaShow2012
    @openarenaShow2012 5 ปีที่แล้ว

    Very well explained, thank you for this video!

  • @biswajitsahoo4391
    @biswajitsahoo4391 5 ปีที่แล้ว

    excellent ur each word bro..could u explain on arraylist,linkedlist,set,vector,queue each in example wise.also how they r interrelated to each other

  • @ramkrishnasitap2498
    @ramkrishnasitap2498 5 ปีที่แล้ว

    Thank you sir :)