java - 卡在哈希表大小加倍的问题上

标签 java hash dictionary

我不知道如何将哈希表的大小加倍。这是代码:

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/

相关文章:

.Net 字典散列对象类型键

Java HashMap、hashCode() equals()——如何实现多个key一致?

c# - 无法散列超过 3GB 的文件

Python 按上次更改值对 dict 进行排序

java - 是否有用于 Java 的远程分析器? (最好使用 JMX)

java - 将对象添加到 ArrayList 时遇到问题

java - Jython 中的类型错误?

java - 是否有可能在 Java 中使用太多泛型类型来分解类?

hash - perl6 哈希键 <$/[0]> 、 <"$/[0]"> 和 {"$/[0]"} 使值的行为不同

c# - 将值分配到键类型为 <Tuple<int,int> C# 的字典中