服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Java源码解析HashMap成员变量

Java源码解析HashMap成员变量

2021-06-28 10:41李灿辉 Java教程

今天小编就为大家分享一篇关于Java源码解析HashMap成员变量,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

本文基于jdk1.8进行分析

关于hashmap的简介,可以参考这篇文章http://www.zzvips.com/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

延伸 · 阅读

精彩推荐
  • Java教程javaweb登录验证码的实现方法

    javaweb登录验证码的实现方法

    这篇文章主要为大家详细介绍了javaweb登录验证码的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    imhxl9692021-02-20
  • Java教程Struts2在打包json格式的懒加载异常问题

    Struts2在打包json格式的懒加载异常问题

    这篇文章主要为大家详细介绍了Struts2在打包json格式的懒加载异常问题,感兴趣的小伙伴们可以参考一下 ...

    eson_151622020-05-13
  • Java教程SpringBoot中使用Filter和Interceptor的示例代码

    SpringBoot中使用Filter和Interceptor的示例代码

    这篇文章主要介绍了SpringBoot中使用Filter和Interceptor的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    克里斯朵夫李维3022019-07-04
  • Java教程Java基础教程之包(package)

    Java基础教程之包(package)

    这篇文章主要介绍了Java基础教程之包(package),本文详细讲解了包的创建、使用等方法,需要的朋友可以参考下 ...

    junjie4582019-11-27
  • Java教程使用SpringBoot整合ssm项目的实例详解

    使用SpringBoot整合ssm项目的实例详解

    Spring Boot 现在已经成为 Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。这篇文章主要介绍了使用SpringBoot整合ssm项目,需要的朋...

    彳亍风8392021-06-10
  • Java教程Spring Boot创建可执行jar包的实例教程

    Spring Boot创建可执行jar包的实例教程

    这篇文章主要介绍了Spring Boot创建可执行jar包的实例教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    马军伟8422021-04-01
  • Java教程Java输入输出流实例详解

    Java输入输出流实例详解

    这篇文章主要介绍了Java输入输出流,结合实例形式详细分析了Java常见的输入输出常用操作技巧与相关注意事项,需要的朋友可以参考下...

    shuair6012021-05-30
  • Java教程java设计模式之简单工厂模式简述

    java设计模式之简单工厂模式简述

    这篇文章主要为大家详细介绍了java设计模式之简单工厂模式,简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类的实例...

    廖为鹏2822020-06-04