好程式設計師Java培訓分享HashMap成員變數解析

好程式設計師發表於2020-10-22

  好程式設計師 Java 培訓 分享 HashMap 成員變數解析 ,首先看一下HashMap 的一些靜態常量。第一個是 DEFAULT_INITIAL_CAPACITY ,預設初始大小, 16 。從註釋中可以瞭解到,大小必須為 2 的指數。這裡的 16 ,採用的 1 左移 4 位實現。而“ aka ”,是 asknownas 的縮寫。

 

/**

 * The default initial capacity - MUST be a power of two.

 **/

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

 

接下來是最大容量,當透過任何一個建構函式的引數隱式指明時使用該值。必須是2 的指數,且小於等於 1<<30 ,即 2 30 次方。

 

/**

 * The maximum capacity, used if a higher value is implicitly specified

 * by either of the constructors with arguments.

 * MUST be a power of two <= 1<<30.

 **/

static final int MAXIMUM_CAPACITY = 1 << 30;

 

接下來是負載因子,預設值為0.75F

 

/**

 * The load factor used when none specified in constructor.

 **/

static final float DEFAULT_LOAD_FACTOR = 0.75f;

 

接下來是和紅黑樹相關的幾個常量。在jdk1.8 中,如果雜湊表中的連結串列太長,就會轉化為一個紅黑樹。

 

TREEIFY_THRESHOLD ,表示要轉為紅黑樹的最小元素個數,即 8 。把紅黑樹轉化為連結串列的門限個數是 6.MIN_TREEIFY_CAPACITY 64 ,表示把連結串列轉化為紅黑樹的最小元素個數。否則,如果太多節點在一個連結串列中時,雜湊表會擴容,而不會轉化為紅黑樹。

 

/**

 * The bin count threshold for using a tree rather than list for a

 * bin. Bins are converted to trees when adding an element to a

 * bin with at least this many nodes. The value must be greater

 * than 2 and should be at least 8 to mesh with assumptions in

 * tree removal about conversion back to plain bins upon

 * shrinkage.

 **/

static final int TREEIFY_THRESHOLD = 8;

/**

 * The bin count threshold for untreeifying a (split) bin during a

 * resize operation. Should be less than TREEIFY_THRESHOLD, and at

 * most 6 to mesh with shrinkage detection under removal.

 **/

static final int UNTREEIFY_THRESHOLD = 6;

/**

 * The smallest table capacity for which bins may be treeified.

 * (Otherwise the table is resized if too many nodes in a bin.)

 * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts

 * between resizing and treeification thresholds.

 **/

static final int MIN_TREEIFY_CAPACITY = 64;

 

接下來是table ,它是儲存 HashMap 的最主要的資料結構,如下圖。從註釋中也可以瞭解到, table 的大小一定是 2 的指數。

 

/**

 * The table, initialized on first use, and resized as

 * necessary. When allocated, length is always a power of two.

 * (We also tolerate length zero in some operations to allow

 * bootstrapping mechanics that are currently not needed.)

 **/

transient Node<K,V>[] table;

 

接下來是entrySet ,如下圖。它儲存快取的對映關係集合。注意, keySet() values() 使用的是父類 AbstractMap 的屬性。

 

/**

 * Holds cached entrySet(). Note that AbstractMap fields are used

 * for keySet() and values().

 **/

transient Set<Map.Entry<K,V>> entrySet;

 

最後是一些其他的屬性,包括HashMap 中元素個數 size ,修改次數 modCount ,下一次進行 resize 的門限個數,以及負載因子 loadFactor ,如下圖。需要注意的是, loadFactor final 的,也就是說,它一旦被賦值,就不能再修改了。

 

/**

 * The number of key-value mappings contained in this map.

 **/

transient int size;

/**

 * The number of times this HashMap has been structurally modified

 * Structural modifications are those that change the number of mappings in

 * the HashMap or otherwise modify its internal structure (e.g.,

 * rehash). This field is used to make iterators on Collection-views of

 * the HashMap fail-fast. (See ConcurrentModificationException).

 **/

transient int modCount;

/**

 * The next size value at which to resize (capacity * load factor).

 * @serial

 **/

// (The javadoc description is true upon serialization.

// Additionally, if the table array has not been allocated, this

// field holds the initial array capacity, or zero signifying

// DEFAULT_INITIAL_CAPACITY.)

int threshold;

/**

 * The load factor for the hash table.

 *

 * @serial

 **/

final float loadFactor;



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69913864/viewspace-2728914/,如需轉載,請註明出處,否則將追究法律責任。

相關文章