Table 3


Set classes and interfaces.

Set (interface) Each element that you add to the Set must be unique; otherwise the Set doesn't add the duplicate element. Objects added to a Set must define equals() to establish object uniqueness. Set has exactly the same interface as Collection. A Set does not guarantee it will maintain its elements in any particular order.
HashSet For all Sets except very small ones. Objects must also define hashCode().
ArraySet A Set backed by an array. Designed for very small Sets, especially those that are frequently created and destroyed. For small Sets, creation and iteration is substantially cheaper than for HashSet. Performance gets quite poor when the Set is large. HashCode() is not required.
TreeSet An ordered Set backed by a red-black tree.1 This way, you can extract an ordered sequence from a Set.
1At the time of this writing, TreeSet had only been announced and was not yet implemented, so there are no examples here that use TreeSet.