Google+ Why Set does not allow duplicate value? ~ Java Geek Zone

Monday 26 October 2015

On 06:03 by Unknown in , , ,    No comments
In most of the interviews of Java, We are often checked with the in depth knowledge of Collection APIs and its capabilities.

One of the very famous interview questions is: Why Set does not allow duplicate value?

Now, don’t tell me you were also asked the same

We all know Set theory in mathematics that all elements in Set are always unique. The same has been applied to the Set API. Let’s try to understand this with very small java code snippet.

Basically I want to add elements in Set where I want to try adding some duplicates as well as shown below.

package com.javageekzone.collection;

import java.util.HashSet;
import java.util.Set;

public class DuplicateSetElements {
public static void main(String[] args) {
 
 Set subjects=new HashSet();
 
 
 subjects.add("Spring");
 subjects.add("Java");
 subjects.add("Oracle");
 
 System.out.println("Subjects: "+subjects);
 
 subjects.add("Java");
 subjects.add("Cobol");
 subjects.add("Spring");
 
 System.out.println("Subjects: "+subjects);
}
}



In above code we are invoking add method to insert the element. Let’s see the source code of this method in the class HashSet.java.

public class HashSet&ltE&gt extends AbstractSet&ltE&gt implements Set&ltE&gt, Cloneable, java.io.Serializable
{
    private transient HashMap&ltE,Object&gt map;
    
    // Dummy value to associate with an Object in the backing Map
    
    private static final Object PRESENT = new Object();
    
    
    
    public HashSet() {
        map = new HashMap<>();
    }
    
    // More Code goes here ,i.e Other methods in Hash Set
    
    
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }
    
    
    
    // More Code goes here ,i.e Other methods in Hash Set
}

Above code is part of rt.jar file in your jre under java/util package. Click here to get full code.

You can see below facts in the HashSet.java
  1. When you instantiate HashSet it’s actually instantiating HashMap in the constructor
  2. PRESENT is the member variable of the HashSet which is instantiated with type Object and it acts as dummy value which it passes to put method.
  3. In the add method implementation internally its calling put method of the HashMap.
  4. put method is considering actual value passed in add method as key and PRESENT as value.
So if I try to add the element “java” by add method.

subject.add(“java”)

It will put that element E here “java” as a key in the HashMap and the dummy value PRESENT as a value to the key.

Now if you look at the code of the HashMap’s put(Key k,Value V) method , you will see put method something like this

public V put(K key, V value) 
{
    //Some code goes here
}

Here we can notice that put (key,value) will
  1. return NULL if the key is unique and the element is added to the map.
  2. return old value of the key if key is duplicate.
Hence , in HashSet add() method ,  it 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 .

Conclusion: HashSet internally utilize HashMap to facilitate the storage and uniqueness of the element.

I hope you are clear with this internal mechanism now. If you still have any doubt please feel free to contact me.

Cheers!!!

0 comments:

Post a Comment