Summary of Java Collections

Source: Internet
Author: User

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. Simply put, HashMap's bottom-up implementation is "a hash table based on the Zipper method". For detailed analysis, please refer to in-depth parsing hashmap, HashTable

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.

-------------------------------------Split Line-----------------------------------------------------

Now we're going to say this list and queue

ArrayList and vectors (including later Arraydeque) are all based on arrays, encapsulating a dynamic, allowed in the allocation object[] array. And the size of this ArrayList and vector, object[] array is ten (arraydeque size is 16).

For Vcetor, this is an older collection, produced in jdk1.0, when Java has not yet been set and the method name is long. Some updates have been made after that, and the method name is short. and ArrayList as the main implementation class of list in the beginning. So better than the vector, after the development process can be used. In this is ArrayList is thread insecure, but you can use the Collections tool class to turn ArrayList into thread-safe.

--

Queue collection, which simulates the queue's "FIFO structure", and the Priorityqueue implementation class is a standard implementation class, it is relatively standard because the elements are not sorted by the order they were added, but rather by the size of the queue elements.

Deque interface is a sub-interface of the queue, he represents a double-ended queue, deque interface typical implementation class: Arraydeque, is based on an array to achieve, the underlying array length of 16

Again, this linkedlist can be used as a queue or as a Cheng.

Finally analyze the performance: for the Common class ArrayList (array-based) and LinkedList (based on linear table), beginners can ignore the poor performance, only know that LinkedList not only provides the list function, but also provides a double-ended queue, the function of the stack.

Discard Insert Delete performance, overall to ArrayList performance and LinkedList, because most can consider using ArrayList. For Arraylist,vector,and LinkedList traversal, Because ArrayList and vectors are implemented based on arrays, they should be accessed using random access method get, and iterators should be used for LinkedList.

=======================

About map in all of the sub-interface and implementation of the map, the most commonly used is hashmap and treemap, in other words, map and set have the same kind of wonderful, detailed can see Api,java source code is first realized Haspmap TreeMap collection, The set class is then implemented by wrapping a map collection with NULL for all value.

First select Haspmap, if you want to keep sort, then use TreeMap

Reference: Uncle Li's "Crazy Java Handout"---

The main article is the notes of their own study, it is inevitable that some statements are not correct or even wrong, welcome to point out, we must be modest to correct.

Summary of Java Collections

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.