我们有一个大小为一百万的HashMap。我们需要存储一百万零一百个对象,但我们不希望HashMap因为只有100个对象而增加到两倍大小(200万)。
编辑: 我想优化HashMap的调整大小。因为仅存储100个对象就需要分配1百万个对象的大小,这是浪费内存的。
我们如何解决这个问题?
编辑: 我想优化HashMap的调整大小。因为仅存储100个对象就需要分配1百万个对象的大小,这是浪费内存的。
我们如何解决这个问题?
HashMap
的容量是2的幂次方,如果2^20(1048576)不够用,你需要使用2^21(2097152)。
编辑:
实际上,你可以通过指定较高的负载因子来控制容量。
如果确切的最大条目数为1000100,则当条目数达到容量 * 负载因子时,HashMap 的容量将加倍。因此,如果容量为1048576,并且你不想将其扩展到2097152,则需要使用约为0.954或更高的负载因子。
因此,使用以下构造函数初始化实例即可:
HashMap<String,Integer> map = new HashMap<> (1048576, 0.954);
相关代码(JDK 6):
public HashMap(int initialCapacity, float loadFactor) {
...
// Find a power of 2 >= initialCapacity
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)(capacity * loadFactor);
table = new Entry[capacity];
...
}
并且
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) // this is what you want to avoid
resize(2 * table.length);
}
Map<Key1, Map<Key2, Value>