Simple parsing of Java collections

Source: Internet
Author: User

OneCollection

1. List

A, ArrayList

B, Vector

C, LinkedList

the first thing to do a simple comparison of the three implementations of List:

With:

Both *arraylist and vectors can be seen as a mutable array;

*arraylist and linkedlist are thread insecure types;

* but both ArrayList and linkedlist can be called by the colletions class. Synchronizedlist () to make ArrayList and linkedlist calls to ensure thread security;

Different:

Although both *arraylist and vectors can be seen as a mutable array, Most of the functions in the vector class contain The Synchronized keyword guarantees thread security;

Although both *arraylist and linkedlist are thread insecure types, linkedlist is actually a storage collection of doubly linked lists, and ArrayList is actually an array;

      *arraylist and vector because they are mutable arrays, the efficiency of data lookup is similar to that of arrays, Because the array stored procedure will have a clear table of the following tables are saved so the relative speed will be faster, but when the data is inserted, the data inserted in the array is actually involved in memory transfer, so when the data is inserted relatively slow, and vector arraylist more slowly;

*linkedlist because it is a doubly linked list, the data is actually stored through the <Node> node, and at the same time it stores a node, it records its previous and subsequent nodes. The first and final nodes are identified in the collection. So when the data is added, in fact, as long as the corresponding node to add points and records before and after the node can be much more efficient than ArrayList, But in the query is, when linkedlist When you need to query the node data for a subscript, you need to traverse the entire collection, and the corresponding subscript of the calculation can be queried, so the linkedlist query is relatively slow.

The Add method of ArrayList indicates that the array is added:

the function implementation of the Vector indicates that it is thread-safe:

LinkedList source Display, performance is a doubly linked list:

Code Show ArrayList and linkedlist Insert and query efficiency comparison:

The output is:

Is this a very surprising result?

2. Set

A, HashSet

B, TreeSet

Simple Analysis . Similarities and differences of HashSet and TreeSet

With:

* All data is stored based on the Map .

* All threads are out of sync

* cannot store duplicate data (because data is stored via The MAP's key value, so duplicate data is not allowed)

* can only be traversed by Iterator.

Vary

*hashset is an unordered collection,TreeSet is an ordered set, and can be sorted according to nature or TreeSet when created. Comparator to sort;

*hashset can store null,TreeSet does not allow nullto be stored ( because CompareTo function,null value cannot be compared, so a null pointer exception is thrown )

*hashset is based on the HashMap implementation,TreeSet is based on TreeMap Implementation

HashSet Basic source code and related inheritance:

TreeSet Basic source code and related inheritance:

TreeSet,add null when source tracking:

Basic traversal of HashSet and TreeSet :

The output is:

TwoMap

The following kinds of MAP implementation classes, mainly have a common point is that the implementation of the map interface, and all through the key-value Key-value pairs form the storage data. Now, let's talk about their individual characteristics.

1, Hashtable

First Hashtable inherits from Dictionaryand realizes Map,cloneable, The Serializable interface, and most of the methods are modified by the synchronized keyword, means thatHashtable is thread-synchronized.

andHashtableof theKey-valuevalues cannot be storedNULLvalue because theputfunction, verify that thevalueif theNULLDirect-throw null pointer, and need to calculateKeyValue ofhashcodevalue, soHashtableis not allowed to storeNULL, and in factHashtableis also throughentry<k, v>object to the data store.:

2, HashMap

first hashmap inherited from abstractmap map , cloneable , serializable interface, but hashmap is not thread-synchronized, of course, from another point of view, hashmap hashtable .

Second HashMap does not prohibit key-value values from storing null values, because the MAP is actually node nodes are stored in the parameters, as shown in the following code:

in addition,HashMapis actually stored in a similar wayExcelthe same space, inHashMapof storage space is a column of small lattice, each column represents aHashvalues, we are storing parameters in theHashMapWhen we need to find out, we will firstKeyValue ofHashvalue to quickly locate the column, and thenequalsmethod to quickly locate the key-value pairs we need. Another point that needs to be added to the note isHashMapusing a hashing algorithm, it is possible to scatterKeyWorthHashvalue range to ensure that too many key-value pairs are not saved as much as possible in the ' same column '.

Source is interested to follow The HashMap Get method, at the bottom, is a recursive function of find :

3, TreeMap

first treemap inherited from abstractmap navigablemap , cloneable , serializable interface, but treemap treemap

the other thing to note is The TreeMap key value is not allowed to store null , because in the TreeMap stored procedure is required through the Key value is called Compare method to make a comparison sort, so the TreeMap stored in the process if Key value is NULL is to report a null pointer exception.

One more thing to keep in mind TreeMap and HashTable are the same as the entry<k, v> objects to store data, but and HashTable not the same,TreeMap storage is closer to the same as a tree list, in the process of storing a Entry node will also be the Entry the left and right side of the node is saved with the parent node.

, PS: If you need a more detailed understanding of The implementation of TREEMAP can query the red-black tree, because TreeMap is based on the red-black tree ( Red-black tree) is implemented.

4, Weakhashmap

Basic functions and HashMap is the same, unlike HashMap , which is weakhashmap is a weak key value pair, meaning roughly explained when When the Weakhashmap key value loses its reference, when java garbage collection occurs,theweakhashmap the corresponding key-value pairs will be cleared:

5, simple display some HashMap,Hashtable 's traverse way

A, HashMap traversal:

HashMap, like TreeMapandWeakhashmap, have the following four ways to traverse :

The output results are as follows:

Ps:hashmap 's keySet can also be traversed by iterator Way and not shown here.

B, Hashtable traversal:

Hashtable in addition to the same four kinds of traversal with HASHMAP, there is a relatively unique way of traversing the next to show:

The code is still a photo, hand knocking good health, nothing or can read more JDK source code there are a lot of places worth learning

Simple parsing 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.