# 112 Internal Working of HashSet in Java | How HashSet works internally in java | Java | RedSysTech

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 พ.ย. 2024

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

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

    Graeat hat's off.......❤

  • @CoconutChronicles-V25
    @CoconutChronicles-V25 2 ปีที่แล้ว +4

    Wow..... Nice good presentation clearly explained :)

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

      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Since you can able to see the code written and executed line by line during this video, I would strongly recomment you to start write the same code in your eclipse and add the breakpoint as necessary and then run the code in debug mode. Let me know if you have any questions, we will help you out at the earliest.
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share with your friends.
      th-cam.com/users/RedSysTech

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

    Impressed 😀

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

      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share with your friends.
      th-cam.com/users/RedSysTech
      Thanks & Regards,
      RedSysTech Team.

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

    You are best Sir

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

      Hi,
      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Thanks & Regards,
      RedSysTech Team.

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

    Wishing all the very best for RedSysTech

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

      Hi @Sujith,
      Thanks for the feedback! Stay tuned for upcoming more videos!!! Kindly Share our videos and Channel with your friends..
      Here is quick link to share..
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Happy learning at #RedSysTech

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

    super sir,,

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

      Thank you for the feedback! Really Appreciate your comments!!! It's means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Regards,
      RedSysTech.

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

    Nice explanation tq anna

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

      Thank you for the feedback! Really Appreciate your comments!!! It's means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Regards,
      RedSysTech.

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

    neatly explained..all the very best for future videos

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

      Hi Sridar,
      Thanks for the feedback! Really Appreciate your words!!! It's means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html

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

    Good explanation sir 👍

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

      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/users/RedSysTech
      Thanks & Regards,
      RedSysTech Team.

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

    pretty clear explanation, only one thing lower the noise of notification bell at the end volume is too much

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

      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Thanks & Regards,
      RedSysTech Team.

  • @amitkumar-xh2fb
    @amitkumar-xh2fb 3 ปีที่แล้ว +1

    thank you

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

      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Regards,
      RedSysTech Team.

  • @SaiKumarVukkum
    @SaiKumarVukkum 4 หลายเดือนก่อน

    Hello sir, if we add two times null value to set. the index will be 0 obviously.it will be verifying with .equals() method right. then why we are not getting null pointer exception?

    • @RedSysTech
      @RedSysTech  3 หลายเดือนก่อน +1

      Hi there,
      Here you go...
      You're absolutely right about null being a tricky case with HashSet in Java. Here's why you don't get a NullPointerException when adding null twice to a HashSet:
      Understanding HashSet and equals():
      HashSet uses a hashing mechanism to store and retrieve elements.
      While adding an element, it calculates a hash code based on the object's equals() method. This hash code is used to determine the bucket where the element will be stored.
      However, the equals() method is not called during the initial addition of null to the set.
      Why No NullPointerException?
      Sets, including HashSet, don't allow duplicate elements.
      When you add the first null to the set, it calculates a hash code for null (which is always the same) and stores it in the appropriate bucket.
      Since null itself doesn't have an overloaded equals() method, it's not invoked during the first addition.
      When you add the second null, the same hash code is calculated, and the set checks if an element with that hash code already exists.
      Because the first null is already present, the set simply doesn't add the second null. It recognizes it as a duplicate based on the hash code, even though equals() isn't called in this case.
      Hope this is clear to you now and helps! Please reach to us in what's app at +91-6383-787-989 for any further questions or clarifications required.
      Regards,
      RedSysTech Tech Support Team.

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

    Informative. Keep it up. Today my interviewer asked same question. That's Y I am here. :D

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

      Thank you for the feedback! All the best for your carrier.
      Really Appreciate your comments!!! It's means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/channels/AFbM5bjpV_eN-TZISuENcQ.html
      Regards,
      RedSysTech.

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

    Here how hashcode call internally based on what formula it will give the hascode and if hashcode will return same what will happen.
    Can you please answer my question if you get a free time

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

      Thanks for asking a question! Will check out and respond back ASAP!

    • @RedSysTech
      @RedSysTech  11 หลายเดือนก่อน

      Hello Java Aspirant!
      Here is the detailed explanation. I tried my best to answer both of your questions.
      In a HashSet, the hashCode method is used to determine the bucket (index in the internal array) where an element will be stored. The general idea is to distribute elements across buckets evenly to achieve efficient insertion, removal, and retrieval.
      Here are the key points regarding the hashCode method and its interaction with HashSet:
      When you add an element to a HashSet, the hashCode method of the object is called to obtain a hash code.
      The hash code is then processed (typically by applying a supplemental hash function) to ensure a more uniform distribution across the buckets.
      The processed hash code is used to calculate the index (bucket) in the internal array where the element will be stored. This index is usually determined by taking the modulo of the hash code with the current capacity of the array.
      If two different elements have the same hash code (a collision), they will end up in the same bucket.
      HashSet handles collisions through a mechanism called chaining. Each bucket is implemented as a linked list of elements that share the same hash code.
      When you add an element to a HashSet, the hashCode method of the object is called to obtain a hash code.
      The hash code is then processed (typically by applying a supplemental hash function) to ensure a more uniform distribution across the buckets.
      If a new element has the same hash code as an existing element in a bucket, it is added to the linked list at that bucket.
      Once elements are in the same bucket, their equality is checked using the equals method to ensure that the HashSet only stores unique elements.
      If equals returns true for any two elements in the same bucket, the HashSet assumes they are equal and treats them as duplicates.
      As elements are added and the load factor increases (the ratio of the number of elements to the capacity of the array), HashSet may decide to resize and rehash the internal array to maintain efficiency.
      During rehashing, the elements are redistributed into a larger array, and the hashing process is applied again based on the new capacity.
      Hope this clarifies! Please let us know if you still having any questions regarding this concept! Always welcome to RedSysTech family!!
      Thanks,
      RedSysTech team.

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

    what will threshold value do

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

      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Here is the answer for you:-
      In Java, we have the concept called threshold in HashMap. It's just thee current capaciity and thee load factor. This is nothing but whenever the capacity fo your hashmap reaches 12th (key-value pair) element, it increase it's capaccity internally from 16 to 32. This is exactly you are watching in this video as well. Hope this helps!!! Please share it with your friends.
      Since you can able to see the code written and executed during this video, I would strongly recomment you to start write the same code in your eclipse and add the breakpoint as necessary and then run the code in debug mode. Let me know if you have any questions, we will help you out at the earliest.
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share with your friends.
      th-cam.com/users/RedSysTech

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

    how hash set use remove method?

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

      Will explain this in another video shortly! Plz stay tuned!! 👍

    • @RedSysTech
      @RedSysTech  11 หลายเดือนก่อน

      Hello Java Aspirant!
      Here is the detailed explanation for the remove method in hashset.
      The remove method in a hash set involves applying the hash function, finding the index, and then handling any collisions that may have occurred during insertion. The exact details of collision resolution depend on the specific implementation of the hash set (chaining, open addressing, etc.).
      Also, the remove method in a hash set is used to delete an element.
      The steps involved in the removal process are as follows:
      1. Apply the hash function to the element to determine its hash code.
      2. Use the hash code to find the index in the array.
      3. If the element is found at the computed index, remove it.
      4. If there are multiple elements in the same bucket (due to collisions), the hash set needs to search through those elements to find and remove the specific element.
      Hope this clarifies! Please let us know if you still having any questions regarding this concept! Always welcome to RedSysTech family!!
      Thanks,
      RedSysTech team.

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

    Why it is not inserting duplicates ?

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

      Hi Karthick,
      Thank you for the feedback!
      Really Appreciate your comments!!! It means a lot for us...
      Stay tuned for upcoming more videos!!!
      Kindly Share our videos and Channel with your friends..
      Here is the quick link to share.
      th-cam.com/users/RedSysTech
      As you know, HashSet is a class which implements Set interface. As per the language design, Set won't allow duplicates, which means when you try to add a duplicate to a set, the duplicate is simply ignored, and the set remains unchanged.
      Also, this does not lead to any compile or runtime errors. in other words, the duplicates are silently ignored.
      This is the underlying reason that HashSet is NOT allowing duplicate insertion.
      As explained in this video, When you call add(E e), method of set, it internally call put(E, e) method of HashMap which looks something like this :
      public boolean add(E e) {
      return map.put(e, PRESENT)==null;
      }
      Hence, the element you are adding to set/HashSet, is internally added to Map as a key. As we need to associate some value with the key in so dummy value(new Object()) PRESENT is passed every time (as Map can contain more than one duplicate values ).
      Now if you closely examine the return map.put(e, PRESENT)==null; of add(e, E) method. There can be two possibilities :
      if map.put(k,v) returns null ,then map.put(e, PRESENT)==null; will return true and element will be added.
      if map.put(k,v) returns old value for the key ,then map.put(e, PRESENT)==null; will return false and element will not be added.
      Hope this helps!!!
      Thanks & Regards,
      RedSysTech Team.