first, several classes to implement set
HashSet, linkedhashset, TreeSet, concurrentskiplistset, copyonwriterarrayset
second, corresponding to the underlying data structure
HashSet-hash table
Linkedhashset-hash table and linked list
TreeSet-red and black Trees
Concurrentskiplistset-jump table
Copyonwritearrayset-list of arrays
third, the application Scenario
Here first, from the element is ordered, in what order to apply the set selection, and finally from the thread safety point of view set Choice.
1, whether orderly
HashSet:
It is recommended to use HashSet if there is no sequential requirement for set Insertion. HashSet is actually using an instance of HashMap (the key value is an element, value is a meaningless object, and the next chapter details hashmap). The HashSet bottom uses hash hashes to store elements in different places (zipper method).
If you need to be orderly, consider Linkedhashset and treeset.
2, in what order to save the set elements
Linkedhashset on the basis of hashset, a doubly linked list is used to record the order of Insertions.
TreeSet is implemented using a tree structure (red-black tree algorithm), which means that TreeSet can customize the collation of the elements in the Set.
3. is thread safe
The set collection gives the Concurrentskiplistset and Copyonwritearrayset two Thread-safe Classes.
One of the copyonwritearrayset, like the previous article, uses read-write separation to preserve the "snapshot" approach, so the traversal is more efficient and does not conflict with other THREADS.
Concurrentskiplistset uses the data structure of the jump table, it can guarantee that the operation of contains, add, Remove is atomic, and is safe in multi-threading environment, but it can't guarantee addall, removeall, Containsall These bulk operations are secure in a multithreaded Environment.
[knowledge collation] Java Collection (ii)-set