How Set in java collection maintaining uniqueness - how HashSet in java implement unique values

/
0 Comments
In your collection implementation how you will make sure the unique values without implementing set/hashSet interface? otherway how you implement set interface in your java collection data?

For example


public class Test {
    
    public static void main(String[] args)
    {
       
        HashSet<Object> testSet= new HashSet<Object>();
        testSet.add(100);
        testSet.add("hello");
        System.out.println("Result "+testSet);
    }
}

The result will be : Result [100, hello ] 

Now we will add some duplicate elements in the same collection list


public class Test {
    
    public static void main(String[] args)
    {
       
        HashSet<Object> testSet= new HashSet<Object>();
        testSet.add(100);
        testSet.add("hello");
        testSet.add(100);
        testSet.add("hello");
System.out.println("Result "+testSet); } }

The result will be : Result [100, hello ] 

Eventhough when we add duplicate element in this collection set with add method why the duplicated data not inserted? because add method returs false when we try to add duplicate elements. How the add method returns false?
The HashSet implementation of the add() method in java is internally implemented by HashMap implementation.

Whenever we tries to create an object of HashSet , it internally create an object of HashMap 

public class HashSet
extends AbstractSet
implements Set, Cloneable, java.io.Serializable

{
    private transient HashMap map;
           
    private static final Object PRESENT = new Object();
        
                public HashSet() {
                      map = new HashMap<>();
                    }
    
       
               public boolean add(E e) {
                       return map.put(e, PRESENT)==null;
                   }
    
       

}


The key is unique in hashmap , when we are calling add(Element E) this element E will be added as key in HashMap and the value for key will be added by the java developer api by using the variable PRESENT , it will create ( new Object() ) which is a dummy value referred by Object reference PRESENT

when you are adding a line in HashSet like  hashset.add(100)   what java does internally is that it will put that element E here 100 as a key in the HashMap(created during HashSet object creation) and some dummy value that is Object's object is passed as a value to the key .

The main point to notice in above code is that put (key,value) will return

1.  null , if key is unique and added to the map
2.  Old Value of the key , if key is duplicate

So , in HashSet add() method ,  we check the return value of map.put(key,value) method with null value
i.e.

   public boolean add(E e) {
            return map.put(e, PRESENT)==null;
       }

So , if map.put(key,value) returns null ,then
map.put(e, PRESENT)==null      will return true and element is added to the HashSet.

So , if map.put(key,value) returns old value of the key ,then
map.put(e, PRESENT)==null      will return false and element is  not added to the HashSet .



You may also like

No comments: