我不知道如何将哈希表的大小加倍。这是代码:
private void doubleLength () {
//Remember the old hash table array and allocate a new one 2 times as big
HashMap<K,V> resizedMap = new HashMap<K,V>(map.length * 2);
/*Traverse the old hash table adding each value to the new hash table.
Instead, add it to by applying
hashing/compression again (compression will be DIFFERENT, because the
length of the table is doubled, so we compute the same hash value but
compute the remainder using the DIFFERENT TABLE LENGTH).*/
for (int i = 0; i < map.length; i++) {
for (K key : map[i].entry) { //iterator does not work here
resizedMap.put(key, map[i].get(key)); //should go here
}
}
哈希表是 LN 对象的数组,其中 LN 定义为:
public static class LN<K1,V1> {
public Map.Entry<K1,V1> entry;
public LN<K1,V1> next;
public LN (Map.Entry<K1,V1> e, LN<K1,V1> n)
{entry = e; next = n;}
}
我的类中有一个可迭代对象,但它不允许使用 map[i].entry.entries()。
public Iterable<Map.Entry<K,V>> entries () {
return new Iterable<Map.Entry<K,V>>() {
public Iterator<Map.Entry<K,V>> iterator() {
return new MapEntryIterator();
}
};
}
我非常不知道如何将公共(public) LN[] map 的大小加倍;
最佳答案
当哈希表太满时,HashMap
已经自行调整大小。您不必调整它的大小。
关于java - 卡在哈希表大小加倍的问题上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13423522/