java foundation - (4)

arraylist whether the collection thread safe? How do thread-safe

arraylist collection is thread safe, to make it safe can use the Collections.synchronizedList Remedies obtain ⼀ thread safe
full set of
ArrayList = arrayList new new ArrayList <> ();
arrayList.add ( "888");
List List = Collections.synchronizedList (arrayList);

Collections.synchronizedList is how do thread-safe

Remedies is a collection of original adds genlock
another ⼀ kinds of thread-safe shutter mode is provided under the package Use concurrent collection CopyOnWriteArrayList

What is the problem arraylist the presence of multi-threaded environment? How to deal with it

Arraylist will traverse a multi-threaded environment, concurrent modification abnormal ConcurrentModificationException
Remedies case:
) Collections.synchronizedList (new new ArrayList <> ())
) new new CopyOnWriteArrayList <> ()

HashMap set of characteristics, the principle underlying implementation

In the HashMap is achieved by JDK1.8 (array + + ⼆ tree list) to achieve
therefore especially
preexisting Use START data transfer when put Remedies times do ⼀ hash key by hash operation on Remedies, and finally the original hash value of the key and the original key
hash value ⽆ right sign 16 bitwise XOR result as the hash value returned by the last, the reason for doing this is to obtain the
hash value of the
number of times a more uniform reduction of hash conflicts, in tune Use putVal store our values, determined node node array is created, if
not created, we do not set the HashMap initialCapacity initial capacity and load due Submenu loadFactor, create a default
capacity of 16, a load due Submenu of 0.75 array, then divided in 2 cases
. Save bitwise exclusive-or array is created our hash value returned by the length done as the coordinates of elements in the array, if the bit is
not stored elements, we directly create nodes node storing data
. If there is an element of the index position in the array is divided into in three cases
(1) the same hash value for a key hash value of the key and we want to keep clicks into the data, and the values are the same, that I START stored data
is repeated, in this case, the new value will cover the original value
(2) to determine whether the element acquired TreeNode structure, if it is mounted to the TreeNode
(3) traversing the array table, find the tail node, to mount our data to the node

Collection of difference and Collectios

Help class Collectios collection
Collection ⼀ single set of interfaces

Before JDK1.7, JDK1.7, JDK1.7 HashMap after the realization of different

Before a data structure array JDK1.7 + chain
structure array + + Red Black & tree list after JDK1.7

HashMap data structure

+ + Chain structure array black and red tree

Why Using an array of linked lists + + Black & red tree structure

Array + hash list to resolve the conflict
red black and tree list is too long, the longer the query time

Why do HashMap of key operation times hashcode

The number of hash values ​​to obtain a more uniform reduce hash conflicts

Published 88 original articles · won praise 26 · views 50000 +

Guess you like

Origin blog.csdn.net/weixin_45678915/article/details/104609990