Это видео недоступно.
Сожалеем об этом.

Hashmap in Java | Internal Working of Hashmap in Java | Hashmap Implementation | DSA-One Course #30

Поделиться
HTML-код
  • Опубликовано: 28 июн 2021
  • Hi guys, In this video, we're going to learn how HashMap works internally.
    🥳 Join our Telegram Community:
    Telegram channel: telegram.me/realanujbhaiya
    Telegram group: telegram.me/dsa_one
    🚀 Follow me on:
    Instagram: / anuj.kumar.sharma
    Linkedin: / sharma-kumar-anuj
    Twitter: / realanujbhaiya
    💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
    📚 Complete DSA Playlist: • DSA-One Course - The C...
    Complete Android Development Playlist: • Android Development Tu...
    Hashtags:
    #anujbhaiya #dsaone
    Tags:
    hashmap in java
    internal working of hashmap in java
    hashmap internal implementation in java
    how hashmap works internally in java
    hashmap
    hashmap java 8
    internal working of hashmap
    hashmap java
    map in java
    hashmap internal working in java
    hashmap internal working
    anuj bhaiya
    hash map
    hashing in java
    java hashmap
    hashmap implementation
    working of hashmap
    hashmap working
    what is hashmap
    internal implementation of hashmap
    hash map in java
    hashmaps in java
    maps in java
    hashmaps
    hashmap in java in hindi
    hashing
    design hashmap
    hashing in data structure
    hash maps
    internal working of collections in java
    hashmap anuj bhaiya
    mapping in java
    hash map java
    java 8
    hash map internal working
    hashmap in java apna college
    what is hashmap in java
    hashmap implementation in java
    hashtable in java
    java map
    map java
    internal working of hashset
    map interface in java
    hashing java
    hashmap in c++
    how hashmap works
    hashmap interview questions
    hash table in java
    implement hashmap
    java
    what is hash map
    anuj bhaiya java
    dsa
    hashset in java
    hash map in c++
    hashing in c++
    hashset internal implementation in java
    hash table in data structure
    how hashmap works internally
    java 8 features
    hashmap c++
    hashmap vs hashtable
    implementation of hashmap
    hash table
    hashmap working in java
    hashmaps java
    internal working of hashmap in java in hindi
    internal working of hashset in java
    maps java
    what is a hashmap

Комментарии • 204

  • @amitanand3672
    @amitanand3672 3 года назад +107

    Few other questions I have come across which everyone should prepare beforehand:
    1. What is bucketing in hashmap ?
    2. What is loadfactor ?
    3. Why hashmap is not recommended in a multi threaded environment ?
    4. Implement a hashmap using custom class as key
    5. is it mandatary to override equals and hashcode method ? What will happen if not overridden ?
    6. What is the difference between hashmap and concurrent hashmap also explain the working of concurrent hashmap .

    • @mohammadfaizanhashmi4213
      @mohammadfaizanhashmi4213 2 года назад +2

      thanx a lot bro, bohot bohot shukriya bhaijan,,,maine noten kr lia hai

    • @frankfernandes718
      @frankfernandes718 2 года назад +2

      woh jo table tha red me 9:57 woh bucket hai

    • @pankajchaudhari2413
      @pankajchaudhari2413 2 года назад

      Bucketing is nothing but index of that node Array
      Hashmap doesn't contain synchronized method or variable , that's y it is not thread safe, overcome to resolve this problem we are having HashTable (as it contains all synchronized method)

    • @Basukinathkr
      @Basukinathkr 2 года назад +12

      Great Amit. For people who are looking here for the answers -
      Buckets are basically the area that contains values.
      Loadfactor is the threshold for the percentage of the bucket when it is decided that now bucket is going to be full and then the capacity is doubled. By default, loadfactor is 75% and capacity is 16. Totally configurable via constructor.
      The reason a hashmap isn't recommended in a MT env because when multiple threads start to access the data while some thread is updating it, there can be issues.
      Whenever overriding equals, it is mandatory to override hashCode so that every time when equals is called, the values generated by hashCode remain consistent.
      Rest 4 and 6 are explanatory. Can be looked on internet. Easy stuff.

    • @nitishprasadkushwaha
      @nitishprasadkushwaha Год назад +5

      Bucketing in HashMap is the process of storing multiple values in a single location (bucket) of the internal array of the HashMap, which is indexed using a hash function. The hash function calculates the hash code of the key and uses it as an index in the array. If multiple keys have the same hash code, they are stored in the same bucket as linked nodes.
      Load factor is a measure of how full a HashMap is allowed to get before its capacity is automatically increased. It is a float value that ranges from 0.0 to 1.0. When the number of entries in the HashMap exceeds the load factor multiplied by the current capacity, the capacity is increased, and the entries are rehashed.
      HashMap is not recommended in a multi-threaded environment because it is not thread-safe. If multiple threads access a HashMap concurrently, it may result in an inconsistent state of the HashMap, leading to data loss or unexpected results. ConcurrentHashMap is recommended for use in a multi-threaded environment.
      Here's an example implementation of a HashMap using a custom class as a key:
      class Person {
      private String name;
      private int age;

      public Person(String name, int age) {
      this.name = name;
      this.age = age;
      }

      // getters and setters

      @Override
      public int hashCode() {
      return Objects.hash(name, age);
      }

      @Override
      public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof Person)) return false;
      Person person = (Person) o;
      return age == person.age &&
      Objects.equals(name, person.name);
      }
      }
      class CustomHashMap {
      private List[] buckets;
      private int capacity;
      private int size;

      private static class Entry {
      K key;
      V value;

      public Entry(K key, V value) {
      this.key = key;
      this.value = value;
      }

      // getters and setters
      }

      public CustomHashMap(int capacity) {
      this.buckets = new List[capacity];
      this.capacity = capacity;
      this.size = 0;
      }

      public void put(K key, V value) {
      int index = key.hashCode() % capacity;
      if (buckets[index] == null) {
      buckets[index] = new LinkedList();
      }
      for (Entry entry : buckets[index]) {
      if (entry.key.equals(key)) {
      entry.value = value;
      return;
      }
      }
      buckets[index].add(new Entry(key, value));
      size++;
      }

      public V get(K key) {
      int index = key.hashCode() % capacity;
      if (buckets[index] == null) {
      return null;
      }
      for (Entry entry : buckets[index]) {
      if (entry.key.equals(key)) {
      return entry.value;
      }
      }
      return null;
      }

      public int size() {
      return size;
      }

      // other methods
      }
      It is highly recommended to override the equals() and hashCode() methods when using custom objects as keys in a HashMap. If these methods are not overridden, the default implementations in the Object class are used, which compares object references rather than object contents. This can lead to unexpected behavior, where two objects that are considered equal by their contents are not considered equal by the HashMap. As a result, the HashMap may not be able to retrieve the values correctly.

  • @nitishprasadkushwaha
    @nitishprasadkushwaha Год назад +12

    Bucketing in HashMap is the process of storing multiple values in a single location (bucket) of the internal array of the HashMap, which is indexed using a hash function. The hash function calculates the hash code of the key and uses it as an index in the array. If multiple keys have the same hash code, they are stored in the same bucket as linked nodes.
    Load factor is a measure of how full a HashMap is allowed to get before its capacity is automatically increased. It is a float value that ranges from 0.0 to 1.0. When the number of entries in the HashMap exceeds the load factor multiplied by the current capacity, the capacity is increased, and the entries are rehashed.
    HashMap is not recommended in a multi-threaded environment because it is not thread-safe. If multiple threads access a HashMap concurrently, it may result in an inconsistent state of the HashMap, leading to data loss or unexpected results. ConcurrentHashMap is recommended for use in a multi-threaded environment.
    Here's an example implementation of a HashMap using a custom class as a key:
    class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }

    // getters and setters

    @Override
    public int hashCode() {
    return Objects.hash(name, age);
    }

    @Override
    public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Person)) return false;
    Person person = (Person) o;
    return age == person.age &&
    Objects.equals(name, person.name);
    }
    }
    class CustomHashMap {
    private List[] buckets;
    private int capacity;
    private int size;

    private static class Entry {
    K key;
    V value;

    public Entry(K key, V value) {
    this.key = key;
    this.value = value;
    }

    // getters and setters
    }

    public CustomHashMap(int capacity) {
    this.buckets = new List[capacity];
    this.capacity = capacity;
    this.size = 0;
    }

    public void put(K key, V value) {
    int index = key.hashCode() % capacity;
    if (buckets[index] == null) {
    buckets[index] = new LinkedList();
    }
    for (Entry entry : buckets[index]) {
    if (entry.key.equals(key)) {
    entry.value = value;
    return;
    }
    }
    buckets[index].add(new Entry(key, value));
    size++;
    }

    public V get(K key) {
    int index = key.hashCode() % capacity;
    if (buckets[index] == null) {
    return null;
    }
    for (Entry entry : buckets[index]) {
    if (entry.key.equals(key)) {
    return entry.value;
    }
    }
    return null;
    }

    public int size() {
    return size;
    }

    // other methods
    }
    It is highly recommended to override the equals() and hashCode() methods when using custom objects as keys in a HashMap. If these methods are not overridden, the default implementations in the Object class are used, which compares object references rather than object contents. This can lead to unexpected behavior, where two objects that are considered equal by their contents are not considered equal by the HashMap. As a result, the HashMap may not be able to retrieve the values correctly.

  • @tech_wizard9315
    @tech_wizard9315 3 года назад +44

    Please add questions for every video of DSA course which would be enough to crack tech giant's for beginners

  • @DensonGeorge18
    @DensonGeorge18 2 года назад +8

    I am not a beginner. I still find the DSA 1 series very helpful to revise the concepts and learn new things for interviews.

  • @anujpotdar3529
    @anujpotdar3529 2 года назад +7

    I was asked this yesterday, could not answer. I started searching for solutions, most of them were too long and difficult to understand. This one taught the concept very well, keep such awesome videos, and more power to you Anuj!

  • @chetantailor3620
    @chetantailor3620 3 года назад +9

    Another worth 16 minutes 👌💫

  • @Vishal-242
    @Vishal-242 3 года назад +5

    Most clear explanation I have seen

  • @tanmoydutta5846
    @tanmoydutta5846 2 года назад +5

    I was also asked the internal implementation of this in JPMC interview (technical round).

  • @sandeshtiwaris6829
    @sandeshtiwaris6829 2 года назад +1

    Yes, it was helpful

  • @shivisharma4267
    @shivisharma4267 3 года назад +5

    Bhaiya please make a video on roadmap to cloud computing🙏🙏🙏

  • @GuruPrasadShukla
    @GuruPrasadShukla 2 года назад +1

    best video on hashmaps in whole coder community!

  • @nandabawane9117
    @nandabawane9117 2 года назад +2

    No wondor.... Your Videos are always be useful...

  • @adityadev2825
    @adityadev2825 3 года назад +1

    Yes the videos was very helpful

  • @factsEcho--
    @factsEcho-- 3 года назад +1

    Yes video was helpful 🙂🙂

  • @richasharma6361
    @richasharma6361 3 года назад +1

    Hello bhaiya, bhaiya bug bounting ke uper video bhi banye with practical knowledge

  • @AbhayKumar-lb6fl
    @AbhayKumar-lb6fl 3 года назад +1

    Yes, it was helpful.

  • @ramgopalbhajans3982
    @ramgopalbhajans3982 2 года назад +1

    Yes it was helpful.

  • @surajdhotre2889
    @surajdhotre2889 3 года назад +1

    U really gr8 bhaiya.Your video hleping me a lots....god bless u 😇

  • @vivekshokeen1192
    @vivekshokeen1192 2 года назад +1

    YES, IT WAS HELPFUL! bhaiya

  • @jiteshjs99
    @jiteshjs99 5 дней назад

    Great explanation :)

  • @itsShashwat
    @itsShashwat 3 года назад +1

    Great video @Anuj Bhaiya

  • @gmanikantasai3655
    @gmanikantasai3655 4 месяца назад

    Awesome explanation brother💯💯

  • @thebugger7269
    @thebugger7269 10 месяцев назад +1

    Sir please make a video on hash mam and concurrent hash map how perform different internally

  • @8750795655
    @8750795655 2 года назад +2

    Great, very well and easily explained

  • @user-gk7xb6bp7c
    @user-gk7xb6bp7c 6 месяцев назад

    YES, IT WAS HELPFUL

  • @madhusmitamishra7801
    @madhusmitamishra7801 3 года назад +5

    Watched this before 15mins of my interview and interviewer asked the same question. Thank you ☺️.

  • @av98
    @av98 5 месяцев назад +1

    Helpful

  • @antorsaha6065
    @antorsaha6065 2 года назад +3

    Thank you bhaiya for this series.
    This is most useful dsa series.😍

  • @awwush
    @awwush 3 года назад +1

    are Abhi aapka hi videos dekhra tha ARRAYS superb!!

  • @mruduladdipalli5417
    @mruduladdipalli5417 2 года назад

    EK Number Bhai

  • @AjayKumar-tl1qy
    @AjayKumar-tl1qy 2 года назад +2

    Calculating hash and index number are two different things while putting value in the hash map. for index calculation hash map uses (n-1) & hash where n is bucket length. its never uses hash as index number.

  • @raghavendraraviteja1055
    @raghavendraraviteja1055 Год назад

    Thanks, it is helpful

  • @ShankarKumar-ko8lt
    @ShankarKumar-ko8lt 2 года назад

    This video is very Helpful for me Thank you Anuj Bhaiya.

  • @rameezkhan2220
    @rameezkhan2220 2 года назад

    The way of explanation is excellent👌👌👌

  • @surajwaghmare4653
    @surajwaghmare4653 Год назад

    Very Helpful

  • @anshukumari6616
    @anshukumari6616 Год назад

    Yes, it was useful.

  • @venkatasaikrishnamarri1158
    @venkatasaikrishnamarri1158 2 года назад +1

    perfect tutorial

  • @devendersinghrathore8837
    @devendersinghrathore8837 Год назад

    Yes. It is really helpful.

  • @kashisharora1018
    @kashisharora1018 2 года назад

    Very nice explanation

  • @surajchavan1000
    @surajchavan1000 Год назад

    Yes, very helpful

  • @auroshisray9140
    @auroshisray9140 2 года назад +1

    thanks bhaiya!

  • @gurkiratkaur4087
    @gurkiratkaur4087 3 месяца назад

    Very helpful

  • @sumeet115
    @sumeet115 Год назад

    Great explanation. Thanks 👍 ..

  • @shreyanshgupta8959
    @shreyanshgupta8959 2 года назад

    Yes it was Helpful

  • @PAWANKUMAR-el2zk
    @PAWANKUMAR-el2zk 3 года назад +2

    Bhaiya make more such kind of videos on all collection's or tree👍🏻

  • @acoustic_ng8706
    @acoustic_ng8706 3 года назад

    Very helpful ! Thank you Anuj bhai !

  • @itz_me_imraan02
    @itz_me_imraan02 3 года назад +3

    The videos are too gud... Plz try to maintain consistency in DSA course🙏🙏...

  • @rrtt6903
    @rrtt6903 2 года назад

    yes, it was helpful

  • @Grandmaster-e7s
    @Grandmaster-e7s Месяц назад

    yes it was helpful

  • @RupeshYadav-kt5dv
    @RupeshYadav-kt5dv Год назад

    Good job 👏 👍 👌

  • @codehustler8582
    @codehustler8582 2 года назад +1

    YES IT HELPS

  • @chaitanyaasati
    @chaitanyaasati 2 года назад

    Yeah, It helped to understand the concept.

  • @ashapilkhwal6320
    @ashapilkhwal6320 6 месяцев назад

    Awesome content and presentation

  • @rajanwalia4tech
    @rajanwalia4tech 3 года назад +4

    I didn't knew about that java8 implement hashmap using self balancing BST.
    Yes video was helpful.

  • @sonam3531
    @sonam3531 2 года назад

    Thanks , it was good information.

  • @are_amay
    @are_amay 2 года назад

    maza aa gaya bhai

  • @unboxingsillystuffs4920
    @unboxingsillystuffs4920 Год назад

    Thank you Anuj..you explained very well.

  • @ashwithchandra2622
    @ashwithchandra2622 2 года назад

    thanks bhayya
    nice explanation😘

  • @AhamedKabeer-wn1jb
    @AhamedKabeer-wn1jb 3 года назад

    Thank you Bhaiya..

  • @learntocode8000
    @learntocode8000 2 года назад

    Yes, It was Helpful

  • @keshavdubey2659
    @keshavdubey2659 3 года назад +1

    Nice bhaiya
    Bhaiya linked list kb se chalu ho gi??

  • @shashwatsharma9904
    @shashwatsharma9904 3 года назад +1

    next video pls make on working of hashset

  • @nileshmutthe5457
    @nileshmutthe5457 2 года назад

    This was helpful

  • @StudyiMnimal1743
    @StudyiMnimal1743 3 года назад

    great bhaiya

  • @shubhammahawar6206
    @shubhammahawar6206 2 года назад

    Thanks for explaining.

  • @agyaani8060
    @agyaani8060 3 года назад +1

    Thankuu so much bhaiya❤❤

  • @indranilmetiya6972
    @indranilmetiya6972 Год назад

    Yes it was helpful

  • @stith_pragya
    @stith_pragya 11 месяцев назад

    Thank You So Much bhaiya,............🙏🙏🙏

  • @surajwaghmare4653
    @surajwaghmare4653 Год назад

    Yes It was useful

  • @dharmeshkhasiya6737
    @dharmeshkhasiya6737 2 года назад

    Thanks for explaining

  • @meetsoni1938
    @meetsoni1938 2 года назад

    Yes it is helpful 😃

  • @nishantaggarwal830
    @nishantaggarwal830 3 года назад

    It was very interesting and helpful

  • @deev1032
    @deev1032 2 года назад

    very helpful

  • @user-ij9zq9qf8s
    @user-ij9zq9qf8s Год назад

    Amazing explanation, thanks man!

  • @rahulshinde6648
    @rahulshinde6648 Год назад

    Thank you

  • @abilashappat9640
    @abilashappat9640 2 года назад

    Very helpful indeed

  • @debasispaul4410
    @debasispaul4410 2 года назад

    it was helpful

  • @pareeks
    @pareeks 2 года назад +2

    Great video. Two things TBC:
    1. New values are pre-appended, not appended at end.
    2. In case of collision in get(Key), value is not checked for equals() but key is checked.

  • @ashutoshpandey4171
    @ashutoshpandey4171 2 года назад

    Thanks for the effort, anuj bhaiya

  • @er.skelafahmed
    @er.skelafahmed 2 года назад

    Yes bhaiya its really helpful thank you 😀

  • @anuj5927
    @anuj5927 2 года назад

    Yes it was helpful :)

  • @piyushkesharwani1987
    @piyushkesharwani1987 3 года назад +6

    Great one, Please make a video on the Internal working of "HEAP" also.

  • @sathishrajasekar1155
    @sathishrajasekar1155 2 года назад

    Thank you Anuj, The Video was helpful.

  • @tejasbavkar2811
    @tejasbavkar2811 2 года назад

    thank you..

  • @jainikprajapati1632
    @jainikprajapati1632 2 года назад

    Yes it is useful 👍 thanks 😀

  • @shriduttpatel1350
    @shriduttpatel1350 2 года назад

    Thank you @Anuj bhaia. It was really help full. Please do not stop making videos like this😀.

    • @AnujBhaiya
      @AnujBhaiya  2 года назад

      If it was really helpful, I will never stop making videos like this 🤗

  • @nitin5865
    @nitin5865 3 года назад +1

    Perfectly explained 👍

  • @mousumi721
    @mousumi721 3 года назад +1

    Thank you bhaiya for this series🌸.i'm really grateful.

  • @ritikarai1232
    @ritikarai1232 2 года назад +1

    Yes it was helpful😁

  • @amanrai8010
    @amanrai8010 2 года назад

    Thanks

  • @nandiniverma5273
    @nandiniverma5273 2 года назад

    It was helpful , Thankyou for deep Knowledge

  • @shashwatmishra7056
    @shashwatmishra7056 10 месяцев назад +1

    A guy from Amazon Seattle, Washington, interviewed me today asked me the same concept

  • @ranjeet5806
    @ranjeet5806 Год назад

    Great explanation, really helpful.

  • @mrityunjoybarman9098
    @mrityunjoybarman9098 3 года назад

    You always make helpful videos vaiya...

  • @samridhisinha8225
    @samridhisinha8225 3 года назад

    too good bhaiya :)

  • @rahulsrivastava1040
    @rahulsrivastava1040 2 года назад

    Anuj bhaiya op❣️❣️

  • @kalpeshsaubhri
    @kalpeshsaubhri Год назад

    Bhai end me jo BST wala concept btaya usse maja aa gya.

  • @anupamdubey5736
    @anupamdubey5736 3 года назад

    😯😯Where's that CAP/BEANIE! Loved the explanation!!❤❤

  • @harshitsrivastava9903
    @harshitsrivastava9903 3 года назад

    Thanks a lot !!

  • @ankitdubey6813
    @ankitdubey6813 3 года назад +4

    This series is the most useful ❤