ArrayList and HashMap interpretation

ArrayList

Data structures: arrays

Feature

  • Fast Query: An array is a contiguous space, through the first address, you can find an array; you can find an array of one element by index

  • Slow additions: length of the array is fixed, add or delete an element, you must create a new array, copy the data over the original array

  • private static final int DEFAULT_CAPACITY = 10; Initial capacity: 10

  • ArrayList<> list = new ArrayList<>(8); -> to set the length of the underlying array -> initialCapacity = 8

    if (initialCapacity > 0) { // 初始容量大于0
    this.elementData = new Object[initialCapacity]; // 初始化元素数组
    }

Expansion mechanism

When the first additive element, called first ensureCapacityInternal(int minCapacity)method of determining whether an empty array, to confirm the capacity of a minimum set ofminCapacity

  • If the array is empty, set to the minimum capacity minCapacitysetting the default capacity DEFAULT_CAPACITY(capacity: 10)

  • If it is not empty array, call the method ensureExplicitCapacity(int minCapacity)to determine whether additional capacity is needed

    Analyzing conditions: IF (be minCapacity - elementData.length> 0) (be minCapacity: + 1'd actual number of elements in the set)

    • Capacity is needed: call grow(int minCapacity), the original array expansion elementDataof 1.5 times, and then the array is assigned a deep copy (overwrite) the original array elementData.
    • Do not need expansion, the method ends.

HashMap

Underlying data structure is as follows: bit bucket (arrays) , linked lists , black tree

#### HashMap implementation features of

  • hashMap initial length of the array 16 , the Entry hashMap array [] of length a power of 2 , number of array elements of the array is greater than 0.75 (load factor) times the time, it will be twice the expansion
  • put: in the storage element, the first through hashCode () Returns the value obtained Key of hashCode, then hashCode is calculated to obtain the hash value, and finally the modulo (via the hash bit operation: hash & (length-. 1) [efficiency more high] -> equivalent to hash% length) was calculated bit storage barrel (array) which position. (length is the length of the array, and a power of 2)
    • HashCode get value for key
    • The high hashCode involved in computing the recalculated hash value **
    • The calculated ** hash value (table.length - 1) ** & calculation performed (table.length a length of the array)
  • If the same hash value calculated last, places linked list to store the same bit position of the tub (array)
  • When the chain length of> = 8 , it will list transformed into red-black trees store

Why is the initial length of the array is 16 and is a power of 2?

** length of 16 reasons: ** If bucket bucket array initialization settings too, will be a waste of memory space, 16 is the size of a compromise, neither did put on a few elements such as 1,2,3 expansion , also did not like the tens of thousands you can only use a little bit of space so as to cause a lot of waste.

The reason for the power of 2 :% instead of operation can use & operation, improve performance

Why load factor is 0.75?

Load factor is set to 0.75 instead of 1, because the set is too large, the greater will be the key to the chance of a collision bucket with a bucket position may be stored several value value, which increases the search time performance drop set too small nor appropriate if it is 0.1, then 10 barrels, threshold 1, you must put the two key-value pairs expansion, a waste of space.

Published 12 original articles · won praise 0 · Views 57

Guess you like

Origin blog.csdn.net/DavinDeng/article/details/104917885