Detailed HashMap collection framework

Here Insert Picture Description

I. Introduction

  • HashMap using the underlying 数组+链表implementation 线程不安全of the Key-Valuestructure.

II. Principle

  1. Element inserted
    through put () method passing key-value pair, the key first call hashCode () method to return a hash value, modulo the length of the array by a hash value (i.e. index = HashCode(Key) % Array.length) to find the location corresponding to the array, and then traversing the list in the list is determined Is there this Key, if the list already this Key (ie, hash collision occurs, the solution are: open addressing method, list method, and then hashing, establish common overflow area), HashMap using the list method to solve Ha Greek conflict, replacing the old with the new value value value value, if not this key to insert the key-value pair 链表头部(before Java8) or 链表尾部(java8 later).

    开放寻址法:寻找下一个空的散列地址;
    链表法:头插法和尾插法;
    再哈希法:有多个不同的Hash函数,使用第二个,第三个,….,等哈希函数计算地址,直到无冲突;
    建立公共溢出区:将哈希表分为基本表和溢出表两部分,和基本表发生冲突的元素填到溢出表
    
  2. Initialization Size: 16

  3. Expansion mechanism
    (1) factors affecting expansion
    Capacity: HashMap i.e. current length;
    loadFactor: HashMap i.e., load factor, the default is 0.75f.

    (2) to measure the expansion of the condition:HashMap.Size >= Capacity x LoadFactor

    (3) expansion steps
    扩容: Create a new empty Entry array, the array is the original length of 2 times ; :
    ReHashEntry traversing the original array, all the re-Hash Entry into a new array, which is the original purpose as uniform as possible Entry distribution.

  4. Head and tail interpolation interpolation
    "head interpolation": Before Java8, using the first interpolation method, key-value pair is about to be inserted into the head of the list. It may change the position of the element in a new array of expansion when, in the concurrent circumstances may occur 环形链表.

    "Tail interpolation": after before Java8, tail-interpolation method, key-value pair is about to be inserted into the tail of the list. Does not change the position of the new element in the array during the expansion, will not be circular linked list.

  5. java8 improvement
    ; post (1) Java8, using an array of red-black tree to achieve + + chain, greater than the size of the list is equal to 8, will automatically switch to red-black tree; deleted when less than 6, becomes again the list
    before (2) Java8 using the first interpolation; before after Java8, tail-interpolation.

Three. HashMap is thread-safe packaging

  • By the Collections.synchronizedMap () to a security thread HashMap package;
  • Principle: Internal SynchronizedMap maintains a Map and mutex mutex.

IV. Comparison of the HashMap and Hashtable

  • With: the two elements are unordered, the load factor was 0.75;
  • different
HashMap Hashtable
Initial capacity 16 11
Expansion mechanism 2 times 2x +1
father AbstractMap Dictionary
Iterator Iterator Enumerator
Fast / security failure fail-fast (fast failure) fail-safer (safety failure)
Published 70 original articles · won praise 4 · Views 6376

Guess you like

Origin blog.csdn.net/qq_44837912/article/details/104378479