關於Java 調整HashMap的存取性能

 

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html 找到這段話。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.java

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.api

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.oracle

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:app

總結,Hashmap有兩個參數能夠調整,initial capacity and load factor.。另一方面,咱們的Hashmap元素比較多,因此把它的初始化大小調整大一些,應該能夠改善存儲效率。ide

 

另外 Performance ConcurrentHashmap vs HashMap 兩個Map的性能比較能夠參考性能

The summaryui

  • 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.this

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

相關文章
相關標籤/搜索