惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - Johnson_wang

对云服务器挂载cos桶 记录一次xxlJob升级版本导致的 xxl-job remoting error(Connection reset) 记一次NoClassDeffoundEror问题解决过程 nginx配置支持ws,并解决跨域 关于消费端接入dubbo,连接失败问题 RedisTemplate关于key出现前缀\xac\xed\x00\x05t\x00\x0f - Johnson_wang - 博客园 ES 单索引大表拆分 关于ES索引被聚合查询导致filedata堵塞 (pressure too high, (smooth) bulk request circuit break) 发送HTML格式邮件 elasticsearch报错FORBIDDEN/12/index read-only / allow delete spring Boot 相关问题以及修复(后续待补充) mybatis数据加解密处理方案 队列缓存区-db写入 synchronized的实现原理 Scheduler踩坑记录 关于RedisTemplate的map存储踩坑记录 Mybatis 分页插件PageHelper 遇坑 Linux 下 Mysql忘记密码重置 Eclipse MAT和jvisualvm分析内存溢出
关于HashMap的加载因子相关理解
Johnson_wang · 2020-06-10 · via 博客园 - Johnson_wang

HashMap在JDK1.7是以数组加链表的形式组成,JDK1.8后新增了红黑树结构,当链表大于8并且容量大于64时,链表结构会转成红黑树结构。

JDK1.8 之所以会加入红黑树是因为当链表过长是会严重影响HashMap的性能,而红黑树具有快速增删改查的特点。

HashMap源码中包含了一下几个常量的定义:

/**
     * The default initial capacity - MUST be a power of two.
     * 初始化长度
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 
/**
     * 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;

    /**
     * The load factor used when none specified in constructor.
     *  默认扩容加载因子
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    /**
     * 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.
     * 当链表长度大于此值且容量大于64时
     */
    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;

关于加载因子

加载因子也叫作扩容因子,用来判断什么时候进行扩容,假设加载因子为0.75,HashMap的初始容量为16,当HashMap中有16 * 0.75 = 12个容量时,HashMap就会进行扩容。

如果加载因子越大,扩容发生的频率就会比较低,占用空间比较小,但是发生hash冲突的几率会提升,对元素操作时间会增加,运行效率降低;

如果加载因子太小,那么表中的数据将过于稀疏(很多空间还没用,就开始扩容了),对空间造成严重浪费;

而且因为容量默认为2的次方,当加载因子为0.75时,容量和加载因子的乘积为整数。

所以系统默认加载因子取了0.5 -1 之间的0.75.