Java Collection-(set)

Source: Internet
Author: User
Tags set set

Today again review Java Collection class, compared to the previous study, there are a lot of gains, but also remember the brain inside, or to insist on blogging for the sake of.

To get to the point, the first thing is to make it clear that collection and collections,collection are the parent interfaces of List,set and queue interfaces, and collections is a tool class for manipulating collections.

The knowledge of the collection system is as follows:

First of all, set set: Set set is similar to a honeypot, you can throw multiple objects in, and set can not remember the order of adding elements. The set set is basically the same as collection, no additional method is provided, or set is collection. If the Add () element is the same, the flase is returned and the addition fails.

Hashset:hashset stores the elements in the binding according to the hash algorithm, thus having good access and lookup performance.

Linkedhashset: Use a table to maintain elements, because the insertion order of elements needs to be maintained, so performance is slightly lower than hashset.

TreeSet: The data structure of the red-black tree is used to store the collection elements and supports two sorting methods, natural sorting and custom ordering. Note: When using TreeSet, the added objects are guaranteed to implement the comparable interface, otherwise the failure is added and a Classcastexecption exception is thrown. In a nutshell, if you want TreeSet to work properly, you can only add objects of the same type.

Enumset: The collection class that is designed for the enumeration class. The interior is stored as a vector, and the storage form is very compact, and all enumset are small in memory and efficient in object use.

Each set implements class performance analysis: HashSet performance is always higher than treeset, if you want to keep the ordered set, use TreeSet. Linkedhashset is slightly slower than hashset for normal insert deletions, but with a table, traversing Linkedhashset is faster and Enumset is the best performance in a set implementation class, but only elements of the same type can be saved. And all of the set implementation classes are thread insecure.

Reference: Uncle Li's "Crazy Java Handout"

Java Collection-(set)

Related Article

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.