java常用集合: ArrayList/Vector; LinkedList ;HashMap; HashSet; LinkedHashMap;ConcurrentHashMap

1.ArrayList/Vector

The ArrayList:
1. Expansion not expanded to 1.5 times (= elementData of Arrays.copyOf (elementData of, newCapacity);)
2. a dynamic array structure itself can not be realized by a sequence of (modified by the transient), thus serializing the inside is each element of the sequence of traversal.
The Vector:
1.Vector ArrayList underlying data structures and the like, but also a dynamic array to store data. However, the use of synchronized write data to synchronize when add () method, but the cost is large, so Vector is a synchronized container is not a concurrent containers.
(Synchronous Concurrent: https: //blog.csdn.net/XM_no_homework/article/details/103888289)

2.LinkedList

LinkedList is implemented based on the underlying doubly linked list
LinkedList insert, delete, move the pointer are very high efficiency.
Find a need to traverse the query is less efficient.

3.HashMap

Here Insert Picture Description
HashMap list and array-based underlayer is achieved. There are two important parameters:

  • capacity
  • Load factor

The default size of the capacity is 16, the load factor is 0.75, when the HashMap size> 16 * will expansion (capacity and the load factor can be freely adjusted) 0.75 occurs.

hash conflict :
because of the limited length of the array, so there will inevitably be the same of different Key index obtained by calculation, this situation can be solved by using a linked list, the linked list is formed in the HashMap will table [index], the data interpolation using the head insert to the list.

put () and GET () :
GET and put the like, but also the incoming Key index is calculated, if this position is a linked list need to traverse the entire list to find the corresponding element by key.equals (k).

In the JDK1.8 of HashMap optimized: the length after writing hash collision list exceeds a threshold value (default 8) and the table 64 is less than the length (or time expansion), the list will be converted to red-black tree .

Suppose hash serious conflict, then the array behind a long list, then re-time complexity is O (n).

If a red-black tree, the time complexity is O (logn).

Greatly improve query efficiency

4. HashSet

HashSet is not allowed to store a collection of repeat elements, realized by the internal HashMap

HashSet member variables :

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();

Wherein the key value is set is added, value of (private static final Object PRESENT = new Object ();)
when a value is written to duplicate HashSet, value will be overwritten, but the key will not be affected, thus ensuring HashSet in the store not only repeated elements.

5. Laidakedःashanap

LinkedHashMap based HashMap, but with the order to address the needs have sort of scene.

It is inherited from underlying HashMap implementation, constituted by a doubly linked list.

There are two types of LinkedHashMap Sort by:

Sort write sequence.
Sort access order.
Wherein the mobile access according to the value sequentially and each will get access to the end of the list, so that a repeated operation can be obtained in accordance with the access order of the sorted list.

LinkedHashMap in fact, been expanded to HashMap, using the doubly linked list to ensure that the order of

6.ConcurrentHashMap

Structure Figure:
Here Insert Picture Description
ConcurrentHashMap array of Segment, HashEntry composition, and HashMap, as is still the array plus list.

Then access to HashEntry Segment (segment) lock (inherited from Reentrantlock) to access segment

/**
 * Segment 数组,存放数据时首先需要定位到具体的 Segment 中。
 */
final Segment<K,V>[] segments;
transient Set<K> keySet;
transient Set<Map.Entry<K,V>> entrySet;

put()

put () plus lock segment, Segment first by targeting key, then put in a specific corresponding Segment in the
although the value is HashEntry volatile keyword modified, but does not guarantee atomicity concurrent , so put operation when still need to be locked process .

put () procedure:

  • Segment table in the current target HashEntry through the hashcode key.
  • Traverses the HashEntry, if not empty it is determined that the incoming key and the current traversal key for equality, equal to the old value is overwritten.
  • Is not empty and you need to create a HashEntry added to the Segment while will first determine whether the capacity is needed.
  • Finally, we will lift the current lock Segment acquired in 1

get()

get () is not locked , simply to target specific Segment Key after passing through Hash, Hash once again by targeting specific elements.

As the value of property HashEntry volatile keyword is modified to ensure the visibility of memory, so every time the latest value when acquired.

ConcurrentHashMap get method is very efficient, because the whole process does not need to lock

JDK1.8 abandoned in the original segment Segment lock, while the use of CAS + synchronized to ensure the safety of concurrency.

Published 28 original articles · won praise 24 · views 10000 +

Guess you like

Origin blog.csdn.net/XM_no_homework/article/details/103888289
Recommended