38. Performance analysis of each set implementation class

Source: Internet
Author: User

HashSet and TreeSet are the two typical implementations of set, how do you choose HashSet and TreeSet? HashSet performance is always better than treeset (especially the most commonly used additions, query elements, and so on), because TreeSet small additional red-black tree algorithms to maintain the number of elements in the collection. You should use TreeSet only if you need a set that remains sorted, otherwise you should be using HashSet.

HashSet also has a subclass: Linkedhashset, for normal insert, delete operation, Linkedhashset is slightly slower than hashset, which is caused by the additional overhead of maintaining the list, but because of the linked list, Traversing Linkedhashset is faster.

Enumset is the best performance in any set implementation class, but it can only hold an enumeration value of the same enumeration class as a collection element

It must be noted that the three implementation classes of set are thread insecure, and if more than one thread accesses a set collection at the same time, and more than a single thread modifies the set set, you must manually guarantee the synchronization of the set set. The set collection can usually be "wrapped" by the Synchoronizedsortedset method of the Collections tool class.

38. Performance analysis of each set implementation class

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.