This class implements the Set interface, backed by a hash table
(actually a HashMap instance). It makes no guarantees as to the
iteration order of the set; in particular, it does not guarantee that
the order will remain constant over time. This class permits the null
element.
- Lists allow for ordered elements, but searching them is very slow.
- Can speed up search tremendously if you don’t care about ordering.
- Hash tables let you do this. Drawback is that you have no control over how elements are ordered.
- hashCode() computes integer (quickly) which corresponds to position in hash table.
- Independent of other objects in table.
- Hashing can be used to implement several important data structures.
- Simplest of these is HashSet
- add elements with add(Object) method
- contains(Object) is redefined to first look for duplicates.
- if duplicate exists, Object is not added
Posted in: