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:-
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.public boolean remove(Object o) {return map.remove(o)==PRESENT;}
It is interesting to see how HashSet class implements clone method which is reproduced here for reference:
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.
public Object clone() {try {HashSetnewSet = (HashSet ) super.clone(); newSet.map = (HashMap) map.clone(); return newSet;} catch (CloneNotSupportedException e) {throw new InternalError();}}
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