Java源码解析HashMap成员变量
作者:李灿辉 发布时间:2023-04-24 07:20:22
本文基于jdk1.8进行分析
关于HashMap的简介,可以参考这篇文章https://www.jb51.net/article/154177.htm。
首先看一下HashMap的一些静态常量。第一个是DEFAULT_INITIAL_CAPACITY
,默认初始大小,16。从注释中可以了解到,大小必须为2的指数。这里的16,采用的1左移4位实现。而“aka”,是as known as的缩写。
/**
* 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;
This is the end.
来源:https://blog.csdn.net/li_canhui/article/details/85088659


猜你喜欢
- @Order控制配置类/AOP/方法/字段的加载顺序1.AOP加载顺序 @Component &nbs
- 废话不多说了,直接给大家上代码了,具体代码如下所示:代码如下:using System;using System.Collections.G
- 1 什么是WMI?Windows Management Instrumentation (WMI)是可伸缩的系统管理结构,该规范采用一个统一
- 1.替换符的使用(1)在 app-android-defaultConfig (或者多渠道打包)下面可以这样使用android { &nbs
- C/C++的数据类型:一,整型Turbo C: [signed] int 2Byte//有符号数,-32768~32
- 编码&解码 通过下图我们可以了解在javaWeb中有哪些地方有转码:用户想服务器发送一个HTTP请求,需要编码的地方有ur
- 今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑。虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没
- 网络爬虫在信息检索与处理中有很大的作用,是收集网络信息的重要工具。接下来就介绍一下爬虫的简单实现。爬虫的工作流程如下爬虫自指定的URL地址开
- 本文实例讲述了C#定时关闭窗体的方法,分享给大家供大家参考。具体方法如下:public partial class Form2 : Form
- Java接口回调产生接口回调的场景在现实生活中,产生接口回调的场景很简单,比如我主动叫你帮我做一件事,然后你做完这件事之后会通知我,&quo
- 有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示U
- 背景今天学习Springboot,但是用的apache-maven 3.0 ,导入springboot1.5.19 ,Maven项目老是爆红
- 本文实例分析了Android中BaseAdapter的用法。分享给大家供大家参考,具体如下:最近做一个项目,项目中用到了ListView,L
- 1.@RequestMapping的介绍通过@RequestMapping,我们可以把请求地址和方法进行绑定的,可以在类、方法上进行声明。类
- 版本对照各版本的文档说明:https://docs.spring.io/spring-data/elasticsearch/docs/1、在
- 输入方法第一种输入方法:scannerimport java.util.Scanner; // 导入java.util.Scannerpub
- 由于项目需要在NDK中使用网络开发,对于c语言网络开发来说,libcurl库是个很不错的选择,但android系统中并没有自带该库,所以就得
- JNI,Java Native Interface,是 native code 的编程接口。JNI 使 Java 代码程序可以与 nativ
- 前言在前面的学习中,我们基本了解了一些 Canvas 的绘制,那么这一章我们一起复习一下图片的绘制几种方式,和事件的简单交互方式。我们从易到
- 本文实例为大家分享了C语言实现学生成绩管理系统的具体代码,供大家参考,具体内容如下结构体版的学生成绩管理系统主要功能有按1 输入学生信息按2