文档章节

关于Java 调整HashMap的存取性能

Oscarfff
 Oscarfff
发布于 2016/07/14 13:28
字数 566
阅读 53
收藏 2

 

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html 找到这段话。

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

总结,Hashmap有两个参数可以调整,initial capacity and load factor.。另外一方面,我们的Hashmap元素比较多,所以把它的初始化大小调整大一些,应该可以改善存储效率。

 

另外 Performance ConcurrentHashmap vs HashMap 两个Map的性能比较可以参考

The summary

  • Reading and writing from ConcurrentHashMap or HashMap with 1 thread access is similarly effective
  • Writing to ConcurrentHashMap by 6 threads is just a little bit slower than writing to separate HashMaps or ConcurrentHashMap
  • Reading from ConcurrentHashMap by 6 threads is slower (about 3x) than reading from single HashMap or ConcurrentHashMap by a single thread. Still the access time is very, very good.It is 0,13s vs 0,3s for reading 1.000.000 entries from a Map.

To sum up, the performance for ConcurrentHashMap is slightly worse when you need to use it from more than one thread but it is not a critical aspect of performance that you should be worried about.

http://stackoverflow.com/questions/1378310/performance-concurrenthashmap-vs-hashmap

© 著作权归作者所有

共有 人打赏支持
上一篇: sort,awk
Oscarfff
粉丝 74
博文 816
码字总数 97116
作品 0
崇明
后端工程师
私信 提问
ThreadLocal原理深入解析

1. 从一次项目经历说起 在上家公司做spark的任务调度系统时,碰到过这么一个需求: 1.任务由一个线程执行,同时在执行过程中会创建多个线程执行子任务,子线程在执行子任务时又会创建子线程执行子...

takumiCX
2018/07/16
0
0
从 Java 代码到 Java 堆

从 Java 代码到 Java 堆 分析是一种美德,PS原文地址:http://www.ibm.com/developerworks/cn/java/j-codetoheap/ 理解和优化您的应用程序的内存使用 本文将为您提供 Java™ 代码内存使用情况...

北极之北
2016/03/10
568
3
Java核心(四)你不知道的数据集合

导读:Map竟然不属于Java集合框架的子集?队列也和List一样属于集合的三大子集之一?更有队列的正确使用姿势,一起来看吧! Java中的集合通常指的是Collection下的三个集合框架List、Set、Q...

王磊的博客
2018/11/28
0
0
分享一份非常强势的Android面试题

马上步入金九银十了,是时候看一些面试题去鹅厂了,接下来我将分享一些面试题,每天总结一点点,希望对大家有所帮助! ListView和RecyclerView区别 参考链接: blog.csdn.net/shu_lance/a… ...

codeGoogle
2018/08/23
0
0
常见面试题——HashMap工作原理的介绍!

HashMap的工作原理是近年来常见的Java面试题。几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和HashMap之间的区别,那么为何这道面试题如此特殊呢?是因为这道题考...

欧阳海阳
2018/06/24
0
0

没有更多内容

加载失败,请刷新页面

加载更多

sass和less的优缺点

简述 sass和less都是css的预编译处理语言,他们引入了mixins,参数,嵌套规则,运算,颜色,名字空间,作用域,JavaScript赋值等 加快了css开发效率,当然这两者都可以配合gulp和grunt等前端构...

莫西摩西
28分钟前
0
0
信号量与PV操作

在计算机操作系统中,PV操作是进程管理中的难点。 首先应弄清PV操作的含义:PV操作由P操作原语和V操作原语组成(原语是不可中断的过程),对信号量进行操作,具体定义如下: P(S):①将信号...

shzwork
40分钟前
0
0
重新认识网络通信协议

OSI网络分层 应用层 http, smtp,pop3这些都属于应用层协议 为用户的应用程序提供服务 表示层 确保一个系统的应用层发送的信息被另一个系统的应用层接收到 会话层 通过传输层建立数据传输的通...

最胖的瘦子
55分钟前
2
0
【转】分布式数据流的轻量级异步快照

本篇翻译自论文:Lightweight Asynchronous Snapshots for Distributed Dataflows,Flink的容错快照模型即来源于该论文。原文地址:https://arxiv.org/pdf/1506.08603.pdf 分布式数据流的轻量...

yiduwangkai
57分钟前
1
0
java使用反射机制设置私有成员变量的值

写一个方法:public void setProperty(Objectobj, String propertyName, Object value){}, 此方法可将obj对象中名为propertyName的属性的值设置为value。(这里不知道obj对象的propertyNam...

群星纪元
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部