Collection of small notes

List

 

ArrayList (linear sequential storage table)
An array implementation. Space-saving, but the array has a limited capacity. 50% increase in capacity would limit is exceeded, with System.arraycopy copy () to the new array, thus better able to give an estimate of the size of the array.
 Create an array of size 10 when the insert elements default for the first time. Access elements in the array, -get very high standard (i) / set (i, e) performance, which is the basic advantage of the array.
 Added elements -add (e) directly in the end of an array of high performance, but if the pressed header inserting, deleting elements -add (i, e), remove (i), remove (e), the use System.arraycopy ( ) to move the affected part of the elements, the performance deteriorates, which is the basic disadvantage.

 

The LinkedList (linear chain table memory (doubly linked list))
In order to achieve a doubly linked list. List no capacity limit, but doubly linked list itself uses more space, but also require additional list pointer operations.
Press standard access element -get (i) / set (i, e) tragedy to traverse the list pointer is moved to place (half if i> size of the array, will shift from the end).
Insertion, before and after modifying the pointer of the node, but still to traverse when deleting the element position pointer can move to the lower part of the list of the subscript refers, in only two of the operation -add list (), addFirst (), removeLast () or with iterator (remove on) () movement of the pointer can be omitted.

CopyOnWriteArrayList
Concurrent optimization of ArrayList. With CopyOnWrite strategy, when modifying the first copy a snapshot to modify, change completely let the internal pointer to a new array.
Because changes to the snapshot invisible to read it, so only write lock does not lock read, plus the cost of copying expensive, typically less suitable for reading and writing scene. If a higher update frequency, or an array of large, or Collections.synchronizedList (list), all operating with the same lock to guarantee thread safety better.
Increased addIfAbsent (e) method iterates through the array to check whether existing elements, not good performance can be imagined.
supplement
Regardless of implementation, the value returned by the subscript -contains (e), indexOf (e), remove (e) are required to traverse all the elements of comparison, the performance can imagine not very good.
Not sorted by element values ​​SortedList, in a thread-safe class nor ConcurrentLinkedList lock-free algorithms, make do with when and equivalence classes Set the Queue, the List will be missing some unique methods.

 

Map
HashMap (Hash array)
In the Entry [] Hash bucket array of arrays implemented by Key hash value modulo the size of the array can be obtained bucket array subscripts.
Insert element, if the same two bucket falls Key (such as a hash value, and the modulus 17 16 belong to a first hash bucket), the next property Entry implemented using a singly linked list to store a plurality of Entry, Entry into the bucket after the bucket next point to the current Entry.
Hashing the lookup key value of 17, to locate the first hash bucket, bucket and all the elements to traverse the list, one by comparing the key value.
When the number of Entry of 75% of the number of barrels (a lot of articles saying the number of buckets used reaches 75%, but not look at the code), multiply expansion bucket array, and reassign all the original Entry, so here it's best to have one estimate.
Bit modulo arithmetic with (hash & (arrayLength-1)) will be faster, so the size of the array is always N-th power of 2, just give you an initial value such as 17 will turn 32. The initial default value when the first element is placed 16.
iterator down (time) array to traverse the hash bucket, it appears to be out of order.
In JDK8, the new default threshold of 8, when a bucket of Entry exceeds the threshold, but not in a way linked list to store in a red-black tree to speed up the search speed Key.

  

LinkedHashMap

Extended HashMap increase two-way linked list, known as the most memory-intensive data structures. When the support iterator () Entry insertion order to sort (but not updated, if accessOrder property set to true, then all read and write access are considered).

Entry is implemented on the add properties before / after the pointer, upon insertion of the Entry Header prepended to themselves. If all read and write access to be sorted, but also before and after the Entry of before / after stitching up to delete themselves in the list.

 

TreeMap
Red and black tree implementation, limited space See introductory tutorial. Sort Key Iterator Support value (), can be achieved in ascending order Key Comparable interface or by the control of the incoming Comparator. You can imagine, in the trees insertion / deletion of certain elements of the cost is greater than the HashMap.
SortedMap support interfaces such as firstKey (), lastKey () to maximize the smallest key, or sub (fromKey, toKey), tailMap (fromKey) Map of clipping a certain period.

 

 

ConcurrentHashMap
Concurrent optimization of HashMap, default 16 write lock (you can set more), effectively disperse the probability of blocking, but did not read locks.
Segment data structure [], Segment which is the hash bucket array, each Segment a lock. Key to figure it out in what Segment years, then calculate it in which hash bucket.
ConcurrentMap support interfaces, such as putIfAbsent (key, value) and opposite to replace the (key, value), and with the realization of CAS replace (key, oldValue, newValue).
Because no read lock is put / remove action is an atomic action (such as an assignment operation is put to an array element / Entry Pointer), a read operation will not see an intermediate state update operation.

 

ConcurrentSkipListMap
JDK6 new concurrent optimization of SortedMap, in order to achieve SkipList. SkipList is a simplified alternative to red-black tree, is an ordered set of popular algorithms, see the introductory tutorial to space limitations. Concurrent package chosen it because it supports CAS lock-free algorithms, and red-black tree is not good lock-free algorithms.
Very special, it's size () can not just transfer, will traverse to count.
 
supplement
About when null, HashMap and LinkedHashMap is arbitrary, TreeMap not set Comparator key can not be null; ConcurrentHashMap in JDK7 in value can not be null (Why is this?), JDK8 in key and value can not be null; ConcurrentSkipListMap all JDK in key and value can not be null.

 

 

Set
Set almost always inside with a Map to achieve, because the Map of KeySet is a Set, and value is false, all use the same Object. Set feature also inherited those features inside Map implementation.

HashSet: interior is HashMap.

LinkedHashSet: interior is LinkedHashMap.

TreeSet: inside is a TreeMap SortedSet.

ConcurrentSkipListSet: internal concurrent optimization of the SortedSet ConcurrentSkipListMap.

CopyOnWriteArraySet: internal optimization is CopyOnWriteArrayList concurrent Set, using its addIfAbsent () method to achieve weight elements, the performance of the method as described above is very general.

 
Added: seems less a ConcurrentHashSet, that would have been achieved with a simple internal ConcurrentHashMap, but did not provide the JDK happens. Jetty on a self-proclaimed, Guava directly with java.util.Collections.newSetFromMap (new ConcurrentHashMap ()) implementation.

 

 

Queue
Queue at both ends out of the List, it is also possible to implement an array or linked list.
- General Queue -
LinkedList
Yes, in order to achieve a doubly linked list LinkedList both List, also Queue. It is the only null into a Queue allows.
ArrayDeque
Queue to two-way loop through the array to achieve. 2 is a multiple of the size, default is 16.
Ordinary arrays can quickly add elements in the end, in order to support FIFO, quickly remove elements from an array of head ,, so require a special algorithm, see the introductory tutorial to space limitations.

 

PriorityBlockingQueue
Concurrent optimization PriorityQueue unbounded, also based on the binary heap. Use a public read-write locks. Although realized BlockingQueue interfaces, it did not feature any blocking queue will automatically when the expansion is not enough space.
DelayQueue
Interior contains a PriorityQueue, the same is unbounded. Elements required to achieve Delayed interface, you need to return the current from the trigger time how long, less than 0 indicates that the triggers for each call.
pull will use (when) peek () see the elements of team head, check that reaches the trigger time. ScheduledThreadPoolExecutor with a similar structure.
- thread-safe blocking queue -

 

BlockingQueue queue length limited to ensure that producers and consumers will not be far from the speed, to avoid running out of memory. The queue length setting can not be changed. When the queue is full enqueue or dequeue the queue is empty, the effect of different functions as follows: empty, the effect of different functions as follows:
     You may report abnormal returns a Boolean value that may block waiting for the waiting time can be set    
入队    add(e)    offer(e)    put(e)    offer(e, timeout, unit)    
出队    remove()    poll()    take()    poll(timeout, unit)    
View element () peek () None None   

 

ArrayBlockingQueue
BlockingQueue concurrent optimization of fixed-length, Based loop through the array. There is a public read-write locks and notFull, notEmpty two blocked Condition management queue is full or empty when.
LinkedBlockingQueue/LinkedBlockingDeque
May be selected concurrently optimized BlockingQueue long, based on the linked list implementation, the length can be set Integer.MAX_VALUE. Using the feature list of the separated takeLock putLock two locks, continue to use notEmpty, notFull management queue is full or empty the blocked state.

Guess you like

Origin www.cnblogs.com/zlchust/p/11740967.html