Uses Sets when you do not want duplicates and you want a list like structure
Sets are like lists which do not allow duplicates.
Sets often internally use a Map
Has methods like:
boolean add(E e);
Use HashSet when:
you are in a single threaded unsynchronized environment (If not either use Collections.synchronizedSet( Set set ) or better still use CopyOnWriteArraySet if writes are infrequent )
you wish there to be no duplicates
you wish to be able to store nulls
you do not care about the order that you add elements to the map. Order is not reflected in the iterator (LinkedHashSet does maintain order)
the values you are storing have equals() and hashcode() implemented properly
Performance:
O(1) add, remove, contains and size (Constant Time) (assuming hash functionj disperses elements properly among the buckets)
Iteration over the set is pro portional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). It is important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
Characteristics:
You can synchronize the Set using: Collections.synchronizedSet( Set set ).
The iterators returned are fail-fast which means that if the set is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException
Use LinkedHashSet when:
you are in a single threaded unsynchronized environment (If not use Collections.synchronizedSet( Set set )
You wish to remember the order of the elements you placed in the set. The iterator will return the elements by the order they were placed in the set. (If you dont care about insertion order use HashSet instead as it is a little bit more faster)
Performance
O(1) add, contains and remove operations (Constant Time) (assuming hash functionj disperses elements properly among the buckets)
Slower than HashSet due to the added expense of maintaining the linked list, with one exception: Iteration over a LinkedHashSet requires time proportional to the size of the set, regardless of its capacity.
Iteration over values requires time proportional to the size of the Set (Note that HashSet is worse it requires time proportional to the capacity of the Set)
Characteristics
You can synchronize the set using: Collections.synchronizedSet( Set set).
The iterators returned are fail-fast which means that if the set is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException
Use TreeSet when:
you are in a single threaded unsynchronized environment (If not use Collections.synchronizedSet( Set set )
you wish the order of the elements returned by the iterator to be based on the natural order of the elements (requires object to implement Comparable) or you are passing a Comparator into the constructor which determines the ordering)
Performance:
O(log N) fo r add, remove and contains opera tions (Logarithmic Time)
Characteristics:
You can synchronize the set using: Collections.synchronizedSet( Set set ).
The iterators returned are fail-fast which means that if the set is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException
Use EnumSet when:
you are in a single threaded environment (If you require synchronized environment use Collections.synchronizedSet( Set set )
you require no duplicates
you need faster performance than HashSet and are using an Enums as values
you need performance equals to int -based "bit flags" operations
Performance
O(1) get / add operations (Constant Time) (Likely but not guaranteed to be Faster than HashMap)
O(1) Even bulk operations are fast (Constant Time)
Characteristics:
Iterators returned by the collection views are weakly consistent : they will not throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.
Use CopyOnWriteArraySet when:
you require a multi threaded environment
Writes hardly occur but sometimes do. Reads are very common.
Performance
iterators are very fast (there is no synchronisation required as the underlying array is an unchanging snapshot)
add, set, remove are slow as the underlying array has to be recreated
Characteristics
Iterators rely on unchanging snapshots of the array at the time the iterators were constructed.
Use ConcurrentSkipListSet when:
you require a multi threaded environment
you wish the order of the elements returned by the iterator to be based on the natural order of the elements (requires object to implement Comparable) or you are passing a Comparator into the constructor which determines the ordering)
you do not need to perform bulk operations
Performance
O (log n) contains, add, and remove operations (Logarithmic time)
O (n) size operation ( Linear Time) (requires traversal of all elements and may or maybe not correct)
Characteristics
bulk operations addAll , removeAll , retainAll , containsAll , equals , and toArray are not guaranteed to be performed atomically.
Back: Data Structures
Page Author: JD