[knowledge collation] Java Collection (ii)-set

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.