真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java源碼解析HashMap成員變量

本文基于jdk1.8進(jìn)行分析

創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)提供肥西企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為肥西眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

關(guān)于HashMap的簡(jiǎn)介,可以參考這篇文章https://www.jb51.net/article/154177.htm。

首先看一下HashMap的一些靜態(tài)常量。第一個(gè)是DEFAULT_INITIAL_CAPACITY,默認(rèn)初始大小,16。從注釋中可以了解到,大小必須為2的指數(shù)。這里的16,采用的1左移4位實(shí)現(xiàn)。而“aka”,是as known as的縮寫(xiě)。

  /**
   * The default initial capacity - MUST be a power of two.
   **/
  static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

接下來(lái)是最大容量,當(dāng)通過(guò)任何一個(gè)構(gòu)造函數(shù)的參數(shù)隱式指明時(shí)使用該值。必須是2的指數(shù),且小于等于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;

接下來(lái)是負(fù)載因子,默認(rèn)值為0.75F。

  /**
   * The load factor used when none specified in constructor.
   **/
  static final float DEFAULT_LOAD_FACTOR = 0.75f;

接下來(lái)是和紅黑樹(shù)相關(guān)的幾個(gè)常量。在jdk1.8中,如果哈希表中的鏈表太長(zhǎng),就會(huì)轉(zhuǎn)化為一個(gè)紅黑樹(shù)。

TREEIFY_THRESHOLD,表示要轉(zhuǎn)為紅黑樹(shù)的最小元素個(gè)數(shù),即8。把紅黑樹(shù)轉(zhuǎn)化為鏈表的門(mén)限個(gè)數(shù)是6. MIN_TREEIFY_CAPACITY為64,表示把鏈表轉(zhuǎn)化為紅黑樹(shù)的最小元素個(gè)數(shù)。否則,如果太多節(jié)點(diǎn)在一個(gè)鏈表中時(shí),哈希表會(huì)擴(kuò)容,而不會(huì)轉(zhuǎn)化為紅黑樹(shù)。

  /**
   * 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;

接下來(lái)是table,它是保存HashMap的最主要的數(shù)據(jù)結(jié)構(gòu),如下圖。從注釋中也可以了解到,table的大小一定是2的指數(shù)。

  /**
   * 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[] table;

接下來(lái)是entrySet,如下圖。它保存緩存的映射關(guān)系集合。注意,keySet()和values()使用的是父類(lèi)AbstractMap的屬性。

  /**
   * Holds cached entrySet(). Note that AbstractMap fields are used
   * for keySet() and values().
   **/
  transient Set> entrySet;

最后是一些其他的屬性,包括HashMap中元素個(gè)數(shù)size,修改次數(shù)modCount,下一次進(jìn)行resize的門(mén)限個(gè)數(shù),以及負(fù)載因子loadFactor,如下圖。需要注意的是,loadFactor是final的,也就是說(shuō),它一旦被賦值,就不能再修改了。

  /**
   * 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;

This is the end.

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接


當(dāng)前標(biāo)題:Java源碼解析HashMap成員變量
URL網(wǎng)址:http://weahome.cn/article/gcoipd.html

其他資訊

在線咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部