Tags

Jan 24, 2012

How HashSet works in Java


HashSet in Java is implemented to use hash based storage of elements and also has the property of having no duplicate elements at the same time.
When we dig dipper into the implementation of HashSet then we see that a HashMap instance member variable is used which not only provides the capability of hash based storage but also ensures zero duplicates contract.
Since HashMap has the property of having unique keys, HashSet makes use of this property of HashMap and stores the members of HashSet as the keys of internal HashMap instance.

If we open the source of HashSet then we see the following two member variables:-
   public boolean remove(Object o) {
            return map.remove(o)==PRESENT;
    }
Here PRESENT is a dummy object which is used as the value object for every key,value pair inserted into the internal HashMap instance. There is no PRESENT dummy object for every key,value pair and this object is declared as static in the HashSet class.
It is interesting to see how HashSet class implements clone method which is reproduced here for reference:

public Object clone() {
            try {
                HashSet newSet = (HashSet) super.clone();
                newSet.map = (HashMap) map.clone();
                return newSet;
            } catch (CloneNotSupportedException e) {
                throw new InternalError();
            }
    }
Here we see that first the clone of super class which is AbstractSet is invoked and then the internal HashMap reference of HashSet class is cloned.
The HashSet class also implements Serializable interface and takes proper care to customize the serialization operation by
1) providing a custom serial version UID
2) provide readObject and writeObject methods

No comments:

Post a Comment