Set
By Octavio Berlanga, on June 3rd, 2010.
A Set is a collection of items that cannot contain duplicates. Set is an interface (not an abstract class).
Methods hashcode() and equals() are critical for Sets to work correctly because they are used to determine if an item should be added. If you have a Set, you must override Object.hashcode() and Object.equals() in the Task class for the set to work as expected.
HashSet
Implements Set.
Not thread safe.
LinkedHashSet
Extends HashSet.
Not thread safe.
Iterators will return items in the order in which they were added to the set.
CopyOnWriteArraySet
Implements Set.
Is thread safe.
Package is java.util.concurrent.
Wraps an instance of CopyOnWriteArrayList.
Iteration is faster than HashSet.
Insertion is slow because it requires the creation of a new Array.
Search operations are slow. Contains() method is also slow, which probably contributes to slowness during insertion.
Synchronized collections lock the entire collection for all operations; this set does not.
The iterator is backed by an array; not by the CopyOnWriteArraySet. Therefore, the iterator’s remove() method is not implemented.
EnumSet
Implements Set and is an abstract class.
Can’t think of a reason to use this set.
NavigableSet
An interface that extends SortedSet (which extends Set).
Provides polling (retrieving and removing): pollFirst(), pollLast().
Provides close matches through ceiling(), floor(), higher(), lower().
Provides reverse navigation: descendingSet(), descendingIterator().
TreeSet
Implements NavigableSet
Not thread safe
This is a sorted set. Items in this set should implement Comparable. If items don’t implement Comparable, then you must supply a Comparator to the TreeSet constructor.
Can provide a range: subSet(), headSet(), tailSet().
Can provide first() and last() items.
ConcurrentSkipListSet
Implements NavigableSet.
Is thread safe.
Package is java.util.concurrent.
Uses a linked list. Insertion time is constant.