一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java源碼解析HashMap成員變量

Java源碼解析HashMap成員變量

2021-06-28 10:41李燦輝 Java教程

今天小編就為大家分享一篇關于Java源碼解析HashMap成員變量,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

本文基于jdk1.8進行分析

關于hashmap的簡介,可以參考這篇文章http://www.ythuaji.com.cn/article/174037.html

首先看一下hashmap的一些靜態常量。第一個是default_initial_capacity,默認初始大小,16。從注釋中可以了解到,大小必須為2的指數。這里的16,采用的1左移4位實現。而“aka”,是as known as的縮寫。

?
1
2
3
4
/**
 * 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次方。

?
1
2
3
4
5
6
/**
 * 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。

?
1
2
3
4
/**
 * 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,表示把鏈表轉化為紅黑樹的最小元素個數。否則,如果太多節點在一個鏈表中時,哈希表會擴容,而不會轉化為紅黑樹。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 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的指數。

?
1
2
3
4
5
6
7
/**
 * 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的屬性。

?
1
2
3
4
5
/**
 * 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的,也就是說,它一旦被賦值,就不能再修改了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 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.

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/li_canhui/article/details/85088659

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天摸天天操天天爽 | 日韩综合网 | 成人国产网站v片免费观看 成人国产精品视频 | 91在线视频播放 | 国产高清视频一区二区 | 无套大战白嫩乌克兰美女 | 外女思春台湾三级 | 精品视频久久久久 | 亚洲精品无码不卡在线观看 | 激情图片 激情小说 | 欧美兽皇video | 91韩国女主播 | 女人张开腿让男人桶视频免费大全 | 国产精品热久久毛片 | 任我淫 | 亚洲欧美在线免费 | 国产成人综合网亚洲欧美在线 | 妇女澡堂淋浴性 | 青草视频在线观看免费网站 | 欧洲vodafonewi喷潮 | 国产一卡2卡3卡四卡国色天香 | 日韩精品视频在线播放 | 亚洲国产成人久久精品hezyo | 欧美a在线| 国产欧美一区视频在线观看 | 青青青手机在线视频 | 嫩交18xxxx | melody中文字幕 | chanelpreston欧美网站 | 国产精品久久久久久久牛牛 | 成人影院免费在线观看 | 亚洲天堂日韩在线 | aaa免费看| 星空无限传媒视频在线观看视频 | 好男人资源在线观看免费的 | 日本啊v在线观看 | 狠狠插综合网 | 视频二区 素人 欧美 日韩 | 亚洲精品国产乱码AV在线观看 | 91亚洲精品国产自在现线 | 国产成人久久精品区一区二区 |