[Source] Read HashMap

Copyright: https://blog.csdn.net/qq_21852449/article/details/85240801
  1. Data structure
    JDK1.8 large compared to HashMap optimized into a bottom array form a linked list Realization of a red-black tree + array + previous list, when the nodes with fewer links chain, when the node over links when a certain value with a red-black tree.Here Insert Picture Description
  2. Inherit the implementation of
    Here Insert Picture Description
  3. Attributes
	//缺省容量
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    //最大容量
    static final int MAXIMUM_CAPACITY = 1073741824;
    //缺省加载因子
    static final float DEFAULT_LOAD_FACTOR = 0.75F;
    //链表转化为红黑树的阀值
    static final int TREEIFY_THRESHOLD = 8;
    //存放元素的实际数组
    transient HashMap.Node<K, V>[] table;
    transient Set<Entry<K, V>> entrySet;
    //实际存储的key-value数量
    transient int size;
    //修改次数
    transient int modCount;
    //[略]扩容参考值
    int threshold;
    //负载因子
    final float loadFactor;
  1. Construction method
/**
initialCapacity:默认大小16
loadFactor:默认0.75
*/
public HashMap(int var1){...}
public HashMap() {...}
public HashMap(Map<? extends K, ? extends V> var1) {...}
public HashMap(int initialCapacity, float loadFactor){...}
  1. The core method
public V put(K var1, V var2) {...}
public Set<K> keySet() {...}
public V replace(K var1, V var2) {...}
  1. Summary
    1.HashMap combines the advantages of an array and a linked list, using Hash algorithm to speed up access speed, using a hash table to solve the problem of the collision conflict, in which each element of the array is the first node of a single linked list, the list is used to resolve conflicts
    2 .HashMap there are two important parameters: initial capacity and load factor. These two parameters greatly affect the performance of the HashMap. The initial capacity is the length of the hash array, the current load factor = current hash array element / hash array length, the maximum load factor for the maximum number of array elements can be accommodated (default maximum load factor of 0.75), the number of elements in the array when the hash when the loading exceeds the maximum multiplication factor and the capacity to be expansion of hashMap, present in the expansion process hashmap put method, the expansion process has always been to increase the power of 2.
    3.HashMap a generic class, key value, and can be of any type, including the null type. key is a null value pairs are always placed in table [0] is the first node in the list, of course, is not necessarily located in a first node table [0] in.

Guess you like

Origin blog.csdn.net/qq_21852449/article/details/85240801