The data structure of HashMap and ConcurrentHashMap

HashMap:
array and linked list, each data corresponds to a linked list, and operation is performed when
inserting
http://blog.csdn.net/tingting256/article/details/52475422
The default length of the array is 16. When it exceeds 16*0.75=12, the number of groups The length becomes 16*2->called resize,
the basic constructor HashMap(int initialCapacity, float loadFactor) of destroying HashMap has two parameters, which are the initial capacity initialCapacity and the load factor loadFactor.
https://www.cnblogs.com/ITtangtang/p/3948406.html
Two objects with the same meaning, insert the same key:
http://blog.csdn.net/zbuger/article/details/51029898

Why is the array length Yes 16 When calculating the slot point of the card, when it falls into which array, hash(hashCode(key)) & (length-1) -> will fall within the position range
when doing AND operation Binary: Interger.toBinaryString(15); Yes When the capacity reaches 12, the capacity will be expanded. During the expansion, more nodes do not need to recalculate to the new slot. The index of the bucket after the hash is unchanged. Why is 0.75 too low, smaller: frequent expansion. If it is too high, make it larger: expansion is not easy to occur, the probability of array repetition increases, resulting in increased get and put times, and conflicts need to traverse a large number of linked list spaces.








There are >>>, >>> in final int hash (Object key) to
reduce the probability of repeated conflict. In

-depth understanding: https://www.zhihu.com/question/20733617
The "disturbance function" mixes the high and low bits of the original hash code, Increase the randomness of low bits, in order to reduce the probability of array collision, reduce it by 10%.
Java 8 thinks that one perturbation is enough. If you do it 4 times, the marginal utility may not be large. The so-called change to one for efficiency.
When getting the object, get, hash find the array slot, circularly linked list, find the correct key-value pair through the equals() method of the key object, and then return the value object.

HashTable:
The default length of the array is 11
http://blog.csdn.net/xuefeng0707/article/details/40834595
The inherited parent class is different, the
thread safety is different
, whether the key and value allow the null value
, the hash value is different, and the insertion algorithm is also different. Modulo operation, hashmap and operation.
Whether to provide contains method
http://blog.csdn.net/fujiakai/article/details/51585767

ConcurrentHashMap:
http://blog.csdn.net/yan_wenliang/article/details/51029372

segment array, each array is a hashtable, segment lock
When multi-threaded put, when the same array is stored, the thread is locked, and when different arrays are stored, there is no lock, which improves the efficiency.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326252264&siteId=291194637