Java set (list, set, map) features, setmap

Source: Internet
Author: User

Java set (list, set, map) features, setmap

There are a lot of classes related to the set. Generally, they only use common methods for addition, deletion, modification, and query, and their method names are basically the same, so they never know when to use the set,

Today, I am free to sort out materials from the Internet to facilitate future memories.

1. List: sequential linear storage, which can store repeated objects

Thread security method: List list = Collections. synchronizedList (new synchronized List (...));

Slow list: two-way linked list to store index data with slow insertion speed and low thread security (better than security performance)

ArrayList: array-based storage of data index data fast insertion of Data slow thread is not safe

Vector: array-based data index data fast insertion data slow thread safety

Stack: inherits from the Vector to implement a post-import, first-out Stack.

 

2. Set: No sequence, no repeated Elements

HashSet: A Set designed for quick search. The object stored in the HashSet must define hashCode ().

TreeSet: The Set that stores the order. The bottom layer is the tree structure. It can be used to extract ordered sequences from the Set.

LinkedHashSet: query speed with a HashSet, and internal use of the linked list to maintain the order of elements (insert order ). When you use the iterator to traverse the Set, the result is displayed in the order of element insertion.

 

3. Map: The key must be unique.

Synchronization Method: Map m = Collections. synchronizedMap (new TreeMap (...));

Hashtable: Allows thread security for null keys based on Hash

HashMap: hash-based implementation that allows empty keys to be null threads is not safe (basically consistent with Hashtable)

TreeMap: The implementation based on the data structure of the red/black tree does not allow insecure threads with null keys

WeakHashMap: improved HashMap, which implements "weak reference" for the key. If a key is no longer referenced by external entities, it can be recycled by GC.

HashSet and HashMap should be used in addition to TreeSet and TreeMap for sorting, because they are more efficient.

 

Iv. SparseArray <E>: stores data in a binary method (a collection class of Android)

In android, we recommend that you use SparseArray <E> instead of HashMap <Integer, E>.

The use of SparseArray is basically the same as the List, in some do not explain in detail, you can see the http://blog.csdn.net/xyz_fly/article/details/793194

 

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.