博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashMap 与 ConcurrentHashMap 在初始化不同大小容量时,实际分配的空间情况
阅读量:5137 次
发布时间:2019-06-13

本文共 3234 字,大约阅读时间需要 10 分钟。

HashMap.java 

int capacity = 1;    int initialCapacitys[] = {1,2,3,4,5,6,7,8,9,10,11,13,15,16,17,26,31,32,33,50,60,63,64,65};    for(int initialCapacity : initialCapacitys){    while (capacity < initialCapacity)        capacity <<= 1;            System.out.println("initialCapacity:" + initialCapacity + ", capacity:" + capacity);}    /*this.loadFactor = loadFactor;threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);table = new Entry[capacity]; */

 

输出结果:

initialCapacity:1, capacity:1

initialCapacity:2, capacity:2
initialCapacity:3, capacity:4
initialCapacity:4, capacity:4
initialCapacity:5, capacity:8
initialCapacity:6, capacity:8
initialCapacity:7, capacity:8
initialCapacity:8, capacity:8
initialCapacity:9, capacity:16
initialCapacity:10, capacity:16
initialCapacity:11, capacity:16
initialCapacity:13, capacity:16
initialCapacity:15, capacity:16
initialCapacity:16, capacity:16
initialCapacity:17, capacity:32
initialCapacity:26, capacity:32
initialCapacity:31, capacity:32
initialCapacity:32, capacity:32
initialCapacity:33, capacity:64
initialCapacity:50, capacity:64
initialCapacity:60, capacity:64
initialCapacity:63, capacity:64
initialCapacity:64, capacity:64
initialCapacity:65, capacity:128

 

 

 

ConcurrentHashMap.java

 

int concurrencyLevel = 16; //默认是16int MIN_SEGMENT_TABLE_CAPACITY = 2;      int sshift = 0;int ssize = 1;while (ssize < concurrencyLevel) {    ++sshift;    ssize <<= 1;}              int initialCapacitys[] = {1,10,16,31,32,33,63,64,65,127,128,129,256,257,512,513,1024,1025,2048,4096,8192,16384};      for(int initialCapacity : initialCapacitys){    int c = initialCapacity / ssize;    if (c * ssize < initialCapacity)        ++c;              int cap = MIN_SEGMENT_TABLE_CAPACITY;              while (cap < c)        cap <<= 1;              System.out.println("initialCapacity:" + initialCapacity + ", c:" + c + ", cap:" + cap + ", ssize:" + ssize);}      // create segments and segments[0]/*Segment
 s0 =    new Segment
(loadFactor, (int)(cap * loadFactor),                     (HashEntry
[])new HashEntry[cap]);Segment
[] ss = (Segment
[])new Segment[ssize];*/

输出结果:

initialCapacity:1, c:1, cap:2, ssize:16

initialCapacity:10, c:1, cap:2, ssize:16
initialCapacity:16, c:1, cap:2, ssize:16
initialCapacity:31, c:2, cap:2, ssize:16
initialCapacity:32, c:2, cap:2, ssize:16
initialCapacity:33, c:3, cap:4, ssize:16
initialCapacity:63, c:4, cap:4, ssize:16
initialCapacity:64, c:4, cap:4, ssize:16
initialCapacity:65, c:5, cap:8, ssize:16
initialCapacity:127, c:8, cap:8, ssize:16
initialCapacity:128, c:8, cap:8, ssize:16
initialCapacity:129, c:9, cap:16, ssize:16
initialCapacity:256, c:16, cap:16, ssize:16
initialCapacity:257, c:17, cap:32, ssize:16
initialCapacity:512, c:32, cap:32, ssize:16
initialCapacity:513, c:33, cap:64, ssize:16
initialCapacity:1024, c:64, cap:64, ssize:16
initialCapacity:1025, c:65, cap:128, ssize:16
initialCapacity:2048, c:128, cap:128, ssize:16
initialCapacity:4096, c:256, cap:256, ssize:16
initialCapacity:8192, c:512, cap:512, ssize:16
initialCapacity:16384, c:1024, cap:1024, ssize:16

 

 

2013-05-14

 

 

名品推荐:  

 

转载于:https://www.cnblogs.com/personnel/p/4584896.html

你可能感兴趣的文章
(1)数据结构——线性表(数组)实现
查看>>
SpringMyBatis解析2-SqlSessionFactoryBean
查看>>
按照excel文档中的内容在当前cad图纸中自动排布实体
查看>>
Winform开发框架之图表报表在线设计器2-图表-SNF.EasyQuery项目--SNF快速开发平台3.3-Spring.Net.Framework...
查看>>
C#基础第八天-作业-设计类-面向对象方式实现两个帐户之间转账
查看>>
洛谷 P3237 [HNOI2014]米特运输
查看>>
Attributes.Add用途与用法
查看>>
JavaScript面向对象初探——封装和继承
查看>>
L2-001 紧急救援 (dijkstra+dfs回溯路径)
查看>>
【概率】poj 2096:Collecting Bugs
查看>>
javascript 无限分类
查看>>
【自制插件】MMD4Maya
查看>>
解决linux服务器乱码
查看>>
mapbox.gl文字标注算法基本介绍
查看>>
【C++】异常简述(二):C++的异常处理机制
查看>>
web.config在哪里
查看>>
SQL Server 2000 版本支持的最大物理内存量
查看>>
spring IOC装配Bean(注解方式)
查看>>
[面试算法题]有序列表删除节点-leetcode学习之旅(4)
查看>>
SpringBoot系列五:SpringBoot错误处理(数据验证、处理错误页、全局异常)
查看>>