1
2
3
4
5
6
观察jdk中HashMap的源码,我们知道极限值为2的31次方。

void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;//将threshold置为Integer.MAX_VALUE
return;//当HashMap的容量已经是2的31次方的时候,直接返回。
}

Entry[] newTable = new Entry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int)(newCapacity * loadFactor);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
观察jdk中源码可发现当HashMap的容量已经是2的31次方的时候,就不会在进行扩容了。

/**
* The next size value at which to resize (capacity * load factor).
* @serial
*/
int threshold;
1
2
3
4
5
如上为对threshold的定义。

/**
* Adds a new entry with the specified key, value and hash code to
* the specified bucket. It is the responsibility of this
* method to resize the table if appropriate.
*
* Subclass overrides this to alter the behavior of put method.
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
观察如上源码可知,当添加完元素后的容量大于threshold,就调用resize方法。
---------------------
作者:liudezhicsdn
来源:CSDN
原文:https://blog.csdn.net/liudezhicsdn/article/details/51234292?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!

HashMap最大容量为什么是2的32次方的更多相关文章

  1. HashMap的最大容量为什么是2的30次方?

    今天看HashMap的底层实现,发现HashMap的最大容量规定为: // 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换) static final int MAXIMUM_C ...

  2. 为什么hashmap的容量永远要是2的次方

    源码hashmap.java文件中有个函数叫tableSizeFor(),他的作用是,通过-1>>>n-1返回一个大于n的最小二次幂,n为map之前的容量,而函数返回值就是扩容的二次 ...

  3. 2的32次方 分类: C#小技巧 2014-08-05 18:18 406人阅读 评论(0) 收藏

    版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. HashMap原理详解

    HashMap 概述 HashMap 是基于哈希表的 Map 接口的非同步实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.此类不保证映射的顺序,特别是它不保证该顺序恒久不 ...

  5. 深入Java集合学习系列:HashMap的实现原理

    1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变 ...

  6. 深入Java集合学习系列:HashMap的实现原理--转

    原文出自:http://www.cnblogs.com/xwdreamer/archive/2012/06/03/2532832.html 1. HashMap概述: HashMap是基于哈希表的Ma ...

  7. 转:Java HashMap实现详解

    Java HashMap实现详解 转:http://beyond99.blog.51cto.com/1469451/429789 1.    HashMap概述:    HashMap是基于哈希表的M ...

  8. 浅谈HashMap的实现原理

    1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变 ...

  9. 转载 hashmap java8前的原理实现

    http://zhangshixi.iteye.com/blog/672697 1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允 ...

随机推荐

  1. sql通配符+sql中查询条件包含下划线等通配符的写法

    一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...

  2. Robot Framework中的未解之谜

    今天在写测试用例的时候偶然发现了一个问题: 一.看脚本逻辑上没有问题,但是在引用变量的时候不能成功引用,脚本截图如下: 这个是关键字A的截图,没有参数. 此时在case中引用${phonesign}和 ...

  3. SA模板

    #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ; char ...

  4. 创建SSH keys

    1.检查是否已经有SSH Key存在 windows: type "%userprofile%\.ssh\id_rsa.pub" Linux: cat ~/.ssh/id_rsa. ...

  5. 三、fs文件操作模块

    fs模块用于文件的读写等操作. 该模块有如下这些方法: 1.fs.stat() : 检测是文件还是目录 const fs = require('fs'); fs.stat('test.html',fu ...

  6. 钱币兑换问题---hdu1284(完全背包)

    Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法.   Input 每行只有一个正整数N,N小于32768.   ...

  7. try catch finally执行顺序 (return / 变量覆盖)

    finally有return 始终返回finally中的return 抛弃 try 与catch中的return 情况1:try{} catch(){}finally{} return x; try{ ...

  8. Simics 破解 转

    http://www.eetop.cn/blog/html/28/1066428-type-bbs-view-myfav.html http://blog.sina.com.cn/s/blog_538 ...

  9. centos6.5 (linux) 禁用模块 IPV6模块的方法

    装完centos后,默认开启了一些模块.可是有些模块并非我们必须的.比方眼下尚未在中国普及的IPV6 怎样关闭IPV6呢 以下介绍的方法,也能够在关闭其它模块的时候使用 第一步: 查找模块名称 使用命 ...

  10. jquery全局变量---同步请求设置

    1.同步 $.ajaxSetup({ async: false }); 2.异步 $.ajaxSetup({   async: true   }); 3.说明:我们一般使用同步完要恢复异步.由于js默 ...