Java Collection Class Learning notes (Set collection)

Source: Internet
Author: User
Tags set set

The set collection does not allow the same elements to be included, and if an attempt is made to add two identical elements to the same set set, the addition operation fails, and the Append () method returns False, and the new element is not added.

Features of the HashSet class:

    • The order of elements cannot be guaranteed, and the order may differ from the order in which they are added, and may vary.
    • HashSet is not synchronous and must be synchronized if multiple threads access and modify a hashset at the same time.
    • The element value can be null.

Features of the Linkedhashset class:

is a subclass of the HashSet class that determines where an element is stored based on its hashcode value, but maintains the order of the elements using the linked list.

Features of the TreeSet class:

is an implementation class for the SortedSet interface, which ensures that the collection elements are in a sorted state.

Features of the Enumset class:

is a collection class designed for enumeration classes, where all elements must be enumerated values of the specified enumeration type.

The collection elements are ordered to determine the order of the collection elements in the order in which the enumeration values are defined within the enum class.

Inserting a null element is not allowed, otherwise it throws nullpointerexception.

Performance analysis of each set implementation class:

HashSet performance is always better than treeset (especially the most commonly used additions, query elements, and so on).

Linkedhashset is slightly slower than hashset, due to the extra overhead of maintaining the list.

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

The set's three implementation classes HashSet, TreeSet, enumset are not thread-safe, and can be "wrapped" by the Synchronizedsortedset method of the Collections tool class. This is best done at creation time, in case of unintended non-synchronous access to the set collection.

Java Collection Class Learning notes (Set collection)

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.