How HashMap works in Java? With Animation!! whats new in java8 tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2025

ความคิดเห็น • 1K

  • @RickGladwin
    @RickGladwin 5 ปีที่แล้ว +456

    For anyone confused about how the index value is calculated: the “&” operator is a “bitwise AND” operator.
    When it is used, both its operands (the numbers before and after the operator) are converted to binary, and a bitwise “and” is performed on each pair of matching place values in both binary numbers. This results in a new binary number. For example:
    12 & 5 = 4, since
    1100 &
    0101 =
    0100
    And also
    2306996 & 15 = 4, since
    1000110011001110110100 &
    0000000000000000001111 =
    0000000000000000000100

  • @manishsoni7689
    @manishsoni7689 3 ปีที่แล้ว +122

    I think I will add one more point at 7:50,
    When you call put() for the element having the same index or hashcode() it calls equals() method as well to compare the key. If it's same then it will overwrite the previous element and if not then append to the next node.
    This part is very important because once I was asked what will happen if you only implement hashcode() and not equals() or vice versa.

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

      This point is very necessary to complete this good video.

    • @PavanKumar-yj8fu
      @PavanKumar-yj8fu 3 ปีที่แล้ว +2

      Very good point, thanks

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

      Would this mean that without the equals() method call, a HashMap could erroneously overwrite both Key and Value at the same time?

    • @dmitrikonnov922
      @dmitrikonnov922 2 ปีที่แล้ว +1

      @@ArkAngelHFB no, it doesn't make sense to overwrite the key, because its already in the array. Instead, the value is overwritten, because the hashmap doesn't tolerate duplicates. This can be also tricky when we deal with children nodes, for instance with one-to-many relationship. Sometimes one's better of to override children's hashcode() based on their class only, so that all children (objects of the same class) have the same hash code.

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

      @@dmitrikonnov922 But without the equals() call, wouldn't it just assume that since it shared the same hash, it WAS the same key and overwrite it?

  • @sent2anil
    @sent2anil 8 ปีที่แล้ว +54

    This is the best tutorial I found for "How HashMap works"! Demo using animation is remarkable :)

  • @barrettkepler7618
    @barrettkepler7618 2 ปีที่แล้ว +1

    HashMaps finally demystified. Thank you very much

  • @shyamalmunshi
    @shyamalmunshi 8 ปีที่แล้ว +33

    This is probably the best explanation of Hashmap I've come across! Kudos to you!

  • @madhugali
    @madhugali 9 ปีที่แล้ว +139

    I'd like to add a point to the wonderful explanation of HashMap implementation
    When HashMap reaches its threshold limit of .75 or 75% it re-size by creating a new array of double the previous size HashMap, and then start putting every old element into that new bucket array and this is called rehashing and it also applies hash function to find new bucket location.

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

      for any object the index will be 0 to (n-1) when we take mod of (n-1), then why we need to re-index or it re-sizes ???

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

      we need to do it so that get and put operations can be done in O(1). if we dont do it, the size of linked lists would keep on increasing and would eventually increase the cost of operations.

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

      It does not take double size but size equal to next power of 2.

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

      You're implicitly stating that it's double ... as a previous power of 2 ( 2^4) is being multiplied by 2 .

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

      this was important part... yet nit covered

  • @condor1016
    @condor1016 8 ปีที่แล้ว +12

    This HAS to be one of the finest tutorials I have ever seen. The use of the animation was extremely well thought out and makes the subject so clear even a non-developer should understand it! Very well done! Thanks for your efforts Ranjith.

  • @anuraag3103
    @anuraag3103 7 ปีที่แล้ว +1

    This is the best explanation available in youtube for Hashmap...!!

  • @areyoucerealtube
    @areyoucerealtube 8 ปีที่แล้ว +11

    Right on my dude. I've been struggling with one specific piece of the implementation (what we do with the hash code), but this video explains it perfectly. Thank you!!

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

    my friend, I wish i saw this 3 days ago....
    I didnt learn anything new because I have been studying hashMaps pretty aggressively the last 3 days, but my life would have been A BILLION TIMES BETTER if i found your video first. Beautifully detailed and easy to understand!
    TO BEGINNERS, THIS IS THE BEST INTRO TO HASHMAP VIDEO ON TH-cam!

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

    Holy shit I was confused about hash tables for so long and this video simplified in such a nice way.

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

    Awesome explanation .thanks a ton.

  • @saruncse85
    @saruncse85 7 ปีที่แล้ว +8

    I was looking for a good explanation for a long time, and I found today. Thanks, Ranjith.

  • @ke30_
    @ke30_ 2 ปีที่แล้ว +1

    Ty so much this is a great explanation

  • @VinodKumar-ng3kc
    @VinodKumar-ng3kc 5 ปีที่แล้ว +7

    People who has disliked, what are you guys expecting ? This is just perfect explanation!!

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

      :) right

  • @sandeepsingh-de4fg
    @sandeepsingh-de4fg 4 ปีที่แล้ว

    Great explanation. If anybody want to know how HashMap internally works please watch this video once. Thanks for sharing such amazing video.

  • @ranjithramachandran5928
    @ranjithramachandran5928  9 ปีที่แล้ว +74

    HashMap working animation + enhancements for HashMap in Java8 - check out! If you liked, please leave a comment!

    • @chilltouchng
      @chilltouchng 9 ปีที่แล้ว +3

      +Ranjith ramachandran Really good! This is the best tutorial about "How HashMap works"!

    • @ChandrakanthReddySoodiReddy
      @ChandrakanthReddySoodiReddy 8 ปีที่แล้ว

      Very Helpful video on HashMaps. I actually understood how Look up works. Thank you...

    • @atulkumar-hm6yo
      @atulkumar-hm6yo 8 ปีที่แล้ว +4

      Sir,Please share how Hashcode made...!!!!

    • @satyaprakashnayak1363
      @satyaprakashnayak1363 7 ปีที่แล้ว

      Very wonderful tutorial....Can you please also explain in animation How ConcurrentHashMap and ConcurrentLinkedQueue works?

    • @kinjalthehero
      @kinjalthehero 7 ปีที่แล้ว

      Thank you

  • @nishikanttayade7446
    @nishikanttayade7446 7 ปีที่แล้ว

    this is THE BEST EXPLANATION of HashMap...thanks

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

    Great explanation. The only point I feel missed was when two keys have the same hashcode the Node actually overrides the original nodes and doesn't get appended to the Linked list. Your run case could have included this scenario.

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

    hands down this is the best video on how hashmap works internally. Thank you very much.

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

    This is the best video that I have seen by now, however its sad that you have stopped posting now

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

    Excellent explanation with example of how hashmap put and get method works internally.

  • @RiteshPuj
    @RiteshPuj 9 ปีที่แล้ว +3

    I learned a lot about Hashmap from this video. Thanks a lot Ranjith, you have did a great job. :)

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

    Splendid explanation with Animation. I have been using HashMap more than decade. Now, I got clarity how it works internally. Thanks for the commendable work!!!👏

  • @yuyuejiang3278
    @yuyuejiang3278 8 ปีที่แล้ว +20

    This is an in-depth very well explained video! Thanks a lot!

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

      Thanks!

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

      @@ranjithramachandran5928 Hi, Sir, I have a doubt, kindly explain plz.
      My "Eclipse IDE" showing two hash value for key "apple" (and for others key too), i.e, 93029210(index = 10) and 93030097(index = 1). According to formula it should be index=10, but it is placed at index= 1.
      Kindly, explain why we get two hash value for each key, and why its index is 1 and not 10. 🙏🙏🙏

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

      @@ranjithramachandran5928Hi sir Nice Explanation and I have a small doubt,
      could you please explain once,In MAP, If two Entries having same key and same hashcode??
      HashMap map=new HashMap();
      map.put("101","hdfc");
      System.out.println(map.put("101","sbi"));
      //Returning value is = hdfc
      //After iterating Map, and getValue() method returning = sbi
      Could you please explain why it's print 'sbi'',
      How about hashcode and equals method work in this case????

  • @jqyang9406
    @jqyang9406 8 ปีที่แล้ว

    This is just crystal clear explanation!! The best explanation for hashMap!!! And thank you for the information about the updated hashMap in Java 8. That's so cooool and smart!!

  • @RagPeddabudi
    @RagPeddabudi 9 ปีที่แล้ว +8

    this is awesome explanation dude.. The best one for HashMap

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

      😭

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

      R

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

      @@ginny5967 watzup buddy ?

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

      @@ginny5967 wat made u cry

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

    One of the best resource on HashMap. Really stunning !!!

  • @pierryangelo
    @pierryangelo 8 ปีที่แล้ว +7

    Excellent job @Ranjith ramachandran! Keep doing it!

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

    Best video i have ever found in you tube regarding implementation of hash map .Described it clearly

  • @shadabsheikh51
    @shadabsheikh51 8 ปีที่แล้ว +9

    Hey Ranjith
    Very detailed and nicely animated work..It was easy to understand

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

    Amazing explaination and animation!! Love it.

  • @harshavardhan7
    @harshavardhan7 8 ปีที่แล้ว +5

    Dude more videos please :) You are awesome :)

  • @ronak212
    @ronak212 7 ปีที่แล้ว +1

    This got to be the best explanation of Internal working of hasmap that I have ever seen.
    Thanks..!

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

    Correction: Whenever there is collision, the new entry should added as the head of linked list, not tail.

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

      any references ?

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

      p.next = newNode(hash, key, value, null);

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

      Yes. Because the insertion will take O(1) time by inserting at head rather than tail.

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

    Ranjith, You have Awesome teaching skill. Without any doubt , This is the best explanation of Hashmap . I experienced the same ease of my understanding regarding your garbage collection in Java explanation too. Cool.

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

    Hi Ranjith,
    In the first put operation
    scores.put("KING",100). your index comes to 4.
    But according to index = hash % (n-1)
    it should be index = 2306996 % (16-1)
    this gives 11 and you calculated it as 4.
    However, if we put 'n' instead of 'n-1' we will get result as 4.
    So either the formula
    hash % (n-1) needs to be changed to hash % (n), if you are calculating indexes correctly
    or
    if the formula is already correct, your index must come out to be 11.
    need your comment on this. thanks

    • @ranjithramachandran5928
      @ranjithramachandran5928  8 ปีที่แล้ว

      +Vincent Lal Please note the index is calculated as hash & (n-1) which is same as hash % n-1

    • @raju5081
      @raju5081 7 ปีที่แล้ว

      it should be n only as the operation always outputs 0 to 15 which are indexes of the table 0 to 15.

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

      +Vincent Lal Wondering how you can calculate like that. Do it bitwise . 2306996=1000110011001110110100 , (n-1)=(16-1)=15= 1111 , 1000110011001110110100 AND 1111 = 0100 which in decimal is 4.

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

    Wow, this is the best tutorial about itHashMap on TH-cam!

  • @chaitu2037
    @chaitu2037 9 ปีที่แล้ว +167

    Good explanation, but can you remove the recording of yourself, frankly it is a little disturbing to watch your head tilt frequently.

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

    Best explanation from all videos I looked on yt.

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

    During put operation, why did we need to compare the hashcode, only comparing the keys will work. Say for an instance two keys have a same hashcode, in that case comparing with hashcode takes you no where, you have to compare key. May be I am missing something, please correct me I am going somewhere wrong.

    • @etofok
      @etofok 7 ปีที่แล้ว

      keys are objects, you'll need to run .equals(). integers are much faster to compare via "&"

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

      I had asked the same question. :-)

  • @ArdhenduShekharSingh1508
    @ArdhenduShekharSingh1508 8 ปีที่แล้ว

    Thanks Ranjith for tutorial. After referring to tutorial I visited the actual source code of Hashmap and now its pretty much clear about the functioning of put and get method and also about resizing of the table size when it reaches the threshold.

  • @yogeshvbk
    @yogeshvbk 9 ปีที่แล้ว +3

    How hash(k) is calculated? Can you please explain..

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

      +Yogesh Bhat - hash(k) in real implementation is done to address "poorly" implemented hashcode methods in classes, that will cause a bucket(or index) loaded with too many nodes(or in older terms entries). It basically spreads higher bits of actual hashcode to lower order. This is done because the modulo operation produces different results only based on lower order btis.(in base 10, think of numbers at 'ones' position)

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

    very lucid way to present the explanation, awesome, thanks alot.

  • @jonkc3635
    @jonkc3635 8 ปีที่แล้ว +79

    Your face is in the way of the slides. lol

  • @ssbabu78
    @ssbabu78 8 ปีที่แล้ว

    Great and useful information with clear explanation and awesome animation. Thank you.

  • @sangitdhanani2137
    @sangitdhanani2137 9 ปีที่แล้ว +3

    dude stop coming back and forth in to the screen. Its distracting

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

    This is the tutorial on hashmap I have been searching for...great job...very well explained...thank you

  • @NileshPatil-m9l
    @NileshPatil-m9l 5 หลายเดือนก่อน

    Your videos are very thoughtful and to the point You should start making more of such content - Watching this in 2014

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

    The best! ...and I knew nothing about this complex topic before the video, but now it's my favorite, thank you!

  • @arvindaggarwal
    @arvindaggarwal 8 ปีที่แล้ว +1

    Quite awesome..... presentation with nice crystal-clear language.....so nice....so much helpful.....I was so confused from last 4-5 years.....but today its remove all deadlocks of confusions and misunderstanding about hashmap's Internals......

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

    Best tutorial for understanding HashMap. Thanks a lot !

  • @AbhijitDixit-ki1oo
    @AbhijitDixit-ki1oo 10 หลายเดือนก่อน

    Use of animation really helps understand the concept.
    Thank you so much sir 🙏

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

    Best tutorial i have ever seen about Hash Map

  • @tolibisql
    @tolibisql 6 หลายเดือนก่อน

    Best explanation. Perfect

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

    Nicely explained! Thanks, keep creating !!

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

    Really good explanation with the help of animation.

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

    The best explanation for hashmap internal working. Thanks man.

  • @SeekerofYab
    @SeekerofYab 8 ปีที่แล้ว +2

    You've made this really easy to understand, and your animations are super-helpful. Thanks!

  • @shikher123yo
    @shikher123yo 9 ปีที่แล้ว

    perfect video to learn how hashmap works in a very short time.

  • @MikeAetherial
    @MikeAetherial 9 ปีที่แล้ว +1

    This is the first explanation that has made sense to me! Its like an epiphany! Thank you!

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

    One of the best explanation of HashMap

  • @vibhorhadke988
    @vibhorhadke988 8 ปีที่แล้ว

    Ranjith with the animation it's very easy to understand Internal functionality .Great effort and Thanks

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

    Excellent, the animation is Outstanding, great job

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

    This is the best explanation so far

  • @reenalparmar540
    @reenalparmar540 7 ปีที่แล้ว

    Very Good Explanation for Hashmap implementation..Thank you...

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

    So helpful and explained in a very simple way

  • @bikaspatro3329
    @bikaspatro3329 9 ปีที่แล้ว

    Excellent demonstration of HashMap implementation!!

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

    Awesome Explanation , now i got the clear idea of how HashMap internally works, thanks a lot.

  • @ravisahu3395
    @ravisahu3395 9 ปีที่แล้ว

    Great explanation about hashing and collisions...

  • @rs6262
    @rs6262 7 ปีที่แล้ว

    Nice presentation with animation. Thank you...

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

    this is the best explanation on the internet

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

    Now i have got the detailed knowledge of hashmap.... Thanks

  • @dmytromelnychuk1909
    @dmytromelnychuk1909 9 ปีที่แล้ว

    Its the best video about HashMap!

  • @praveen2hearts
    @praveen2hearts 8 ปีที่แล้ว

    Classic video on HashMap... Please keep on posting such classics

  • @maximshen3078
    @maximshen3078 8 ปีที่แล้ว

    Best HashMap explanation and animation !

  • @rohitmanchanda8412
    @rohitmanchanda8412 7 ปีที่แล้ว

    Absolutely fantastic use of animation for presentation. understood in one shot. Thanks for taking the pain of wonderful preparation. excellent work!

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

    Recommended video
    Simply explain how it happens well done

  • @jathitu
    @jathitu 8 ปีที่แล้ว

    Superb video explaining HashMap. Best i have gone through for any topic on Java.
    Please post more videos, would love to hear on many more Java topics from you.

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

    One of the genuine and worthy explanation.

  • @neetishraj
    @neetishraj 7 ปีที่แล้ว

    Thank you Ranjith, you strike right into the root concepts that clear all doubts that branch out from that root confusion. Thank you sir

  • @dillipkumarnayak786
    @dillipkumarnayak786 7 ปีที่แล้ว

    Very Nice explanations Sir thanks a lot... for sharing

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

    Best explanation I have seen so far

  • @MadhuCM
    @MadhuCM 9 ปีที่แล้ว

    Excellent explanation with animation. great going and thanks.

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

    Very well explanation of HashMap .

  • @pankajk2928
    @pankajk2928 7 ปีที่แล้ว

    This is the best explanation for HashMap. Thanks Ranjith.

  • @bavanasudheerkumar
    @bavanasudheerkumar 9 ปีที่แล้ว

    Very clear explanation,good that you discussed the enhancements for hashmap in Java 8.

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

    Earlier i though it would be very complicated concept but sir you made it so simple. thanks

  • @pravinghorpade2771
    @pravinghorpade2771 9 ปีที่แล้ว

    Very Good explanation and Animation...Thanks

  • @pushpal.r.1096
    @pushpal.r.1096 2 ปีที่แล้ว

    Very nice explanation. Thank you Sir.

  • @senthilkumaravel8305
    @senthilkumaravel8305 8 ปีที่แล้ว

    Too good!!! thanks for helping me to understand the internal hashmap work in java

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

    superb explanation. Thank you so much. May be in all java interviews this question is expected. God bless you ..

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

    Thanks for the well explained HashMap

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

    there cannot be a better explanation than this... thanks :)

  • @vipindixitvipin
    @vipindixitvipin 8 ปีที่แล้ว

    Its always a good time while watching your precious and informative videos. Very good and easy to understand. Thanks alot

  • @raazeev1
    @raazeev1 7 ปีที่แล้ว

    The animations help remember things.. Kudos...!! Thanks

  • @kishorejosyula6794
    @kishorejosyula6794 8 ปีที่แล้ว

    One of the best explanations I have seen!!!!

  • @KolapatiRamakrishna69
    @KolapatiRamakrishna69 8 ปีที่แล้ว

    Very nice explanation.. Anyone can easily understand the concept. Thanks a lot.

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

    Amazing tutorial. Super easy to understand. Thanks.

  • @prakashkchauhannn
    @prakashkchauhannn 8 ปีที่แล้ว

    Nice and Simple explanation. Thank you.