This is an old one. In Java, HashSet delegates all its functions to HashMap.
What goes on under the covers of a HashSet<T> is:
- A member variable, map, of type HashMap<T, Object> is maintained.
- All operations on HashSet is delegated to a corresponding operation on HashMap - add(), remove(), contains(), etc.
- A dummy object
add(T objOfTypeT)
Invokes put(objOfTypeT, dummy object)
remove(T objOfTypeT)
Invokes HashMap's remove(objOfTypeT)
size()
returns map's size
contains(T objOfTypeT)
returns map.containsKey(objOfTypeT)
iterator()
returns map's keySet's iterator
No comments:
Post a Comment