java hashmap存取效率_关于Java 调整HashMap的存取性能

An instance of HashMap has two parameters that affect its performance: initial capacityand 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 modifiesthe 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 capacityand load factor.。另外一方面,我们的Hashmap元素比较多,所以把它的初始化大小调整大一些,应该可以改善存储效率。

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值