java · · hashMap data structure

Feature

  • Thread safe
  • HashMap, and Hashtable, SynchronizedMap difference:
    • HashMap thread-safe, can have key values ​​or value null value.
    • hashtable thread-safe, you can not have key values ​​or value null value.
    • ConcurrentHashMap thread-safe, there can be null values ​​or value of key values. Delete operation is relatively time-consuming.
    • SynchronizedMap thread-safe, there can be null values ​​or the value of key values.
      • You can Collections.synchronizedMap(new HashMap<String, Object>())create ways
    • 性能:HashMap>ConcurrentHashMap>SynchronizedMap>Hashtable

Construction method

Related parameters

  • initialCapacity: initial maximum capacity, the default << 1 4 (2 ^ 4), the internal variable threshold is actually used (default size), the actual maximum capacity and no storage.
  • loadFactor: Load factor (default = initial maximum capacity load capacity factor *), default 0.75
  • threshold: default capacity, internal variables, according initialCapacity generated. When the constructor is executed, the input into initialCapacity ^ minimum value of k is not less than the current number of 2, as threshold. When constructing the first table (first put, performs a resize () method), the table size is set threshold, then let threshold = threshold loadFactor; every subsequent resize, the table size is the size = table 2; threshold = threshold * 2;
  • The default relationship: threshold = initialCapacity * loadFactor (equation is not satisfied when the maximum capacity)

Balance and compromise

  • Load factor: the degree of fill hash table of elements, the greater the load factor, the higher utilization of space, the higher the chance of conflict (the higher the cost of a query)

Code analysis

  • public HashMap(int initialCapacity, float loadFactor)
public HashMap(int initialCapacity, float loadFactor) {
        /**初始最大容量为非负整数*/
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        /**
        * static final int MAXIMUM_CAPACITY = 1 << 30;
        * 当 initialCapacity 大于最大容量(2^30,约10.74亿)时,强制设置为容量为最大容量。
        */
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        /**
        * 加载因子为大于0的浮点数
        * public static boolean isNaN(float v) {
        *   return (v != v);
        * }
        * Float.isNaN(loadFactor):NaN(not-a-number),例如. float v = 0.0f/0.0f;
        */
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        /**赋值容量因子*/
        this.loadFactor = loadFactor;
        /**
        * 转换输入的初始最大容量为2^k,赋值给threshold作为实际最大容量
        * 这样做的意义待分析
        */
        this.threshold = tableSizeFor(initialCapacity);
    }

/**
* 获取不小于当前数的最小的2^k的值.
* 例如:31->32,65->128
*/
static final int tableSizeFor(int cap) {
    int n = cap - 1;
    n |= n >>> 1;
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
  • public HashMap(int initialCapacity)
public HashMap(int initialCapacity) {
    this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
  • public HashMap()
/**
* 在resize()方法中设置threshold的值
* newCap = DEFAULT_INITIAL_CAPACITY;
* newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
*/
public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
  • public HashMap(Map<? extends K, ? extends V> m)
    • Be combed
    public HashMap(Map<? extends K, ? extends V> m) {
      this.loadFactor = DEFAULT_LOAD_FACTOR;
      putMapEntries(m, false);
    }

summary:

  • java-bit computing knowledge to be summarized. (Object bit operation to increase the efficiency)
    • 1 << k(2^k)
  • the difference between double and float to be summarized.
  • HashMap、treemap、treenodes关系
  • Why n initialization configuration map, converts the input initial maximum capacity of 2 ^ k, assigned to the actual maximum capacity threshold.

Guess you like

Origin www.cnblogs.com/kunlingou/p/11067857.html