HashSet internal implementation in java
HashSet is one of
the implementation class of Set interface,
it allows any type of elements and every element is unique
in HashSet. Because HashSet does not
allow the duplicate elements and it allows one null value.
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
HashSet<Object>
set=new HashSet<>();
set.add(101);
set.add("java");
set.add(103);
set.add("java");//duplicate
set.add(103); //duplicate
set.add(null);
System.out.println("HashSet :
"+set);
}
}
Output :
HashSet : [null, java, 101, 103]
HashSet retrieves the objects based on hashcode of an object because it follows the principles of HashMap.
What happens internally when we
pass the duplicate elements into the set.add(); method of
HashSet, whenever we add duplicate elements to HashSet it does not allow.
Whenever we create the object for HashSet that internally create object
for HashMap
Internal code of HashSet
public class HashSet<E> extends AbstractSet<E>
implements Set<E>,
Cloneable, Serializable
{
private transient HashMap<E,
Object> map;
private static final Object PRESENT = new Object();
public HashSet()
{
this.map = new HashMap();
}
//
other methods implementation
public boolean add(E paramE)
{
return this.map.put(paramE,
PRESENT) == null;
}
//
other methods implementation
Once look at the above code, in
that public HashSet() constructor
create this.map =
new HashMap(); object.
HashMap does not allow the duplicate keys, so each key is unique. In HashSet, we pass the element
that element is considered as a key in the HashMap.
Now we have to associate a value with the key, so for that java API developers
created Object PRESENT =
new Object(); [new object] for dummy value.
Look at add(E
paramE). Whenever add() method calling internally this.map.put(paramE,
PRESENT) [put()] method will return.
paramE Is take a key
and PRESENT take a value
Whenever we pass element to
add(101) then internally HashMap will take 101 as key and associate a dummy value
with the key.
If HashMap map.put(K
paramK, V paramV) return null
then
HashSet add(E paramE) return true
and an element is added to HashSet.
If HashMap map.put(K
paramK, V paramV) return old
value of the key
then
HashSet add(E paramE) return
false and an element is not added to HashSet.
Actually if we pass
duplicate key in HashMap
map.put(101, "emp1"); //(key1, value1)
map.put(101, "emp2");//(key2, value2)
Whenever we retrieve we will
get output [101,
emp2] (key1, value2), so we missed value1 for that below
Hashmap put() method will
return the old value that is why put() method return type is V (object
type).
public V put(K
paramK, V paramV)
{
//Implementation
}
That is the reason behind
the add() method return false when put()
method return old value of key.
That mean when we pass
duplicate key then put() method will return old value.
I hope you guy it’s clear J
No comments:
Post a Comment