Java 陷入哈希表大小加倍的困境

Java 陷入哈希表大小加倍的困境,java,hash,map,Java,Hash,Map,我不知道如何将哈希表的大小加倍。代码如下: 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 val

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

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
    }

}
private void doubleLength(){
//记住旧的哈希表数组,并分配一个2倍大的新哈希表数组
HashMap resizedMap=新的HashMap(map.length*2);
/*遍历旧哈希表,将每个值添加到新哈希表中。
相反,通过应用将其添加到
再次进行哈希/压缩(压缩将不同,因为
表的长度是原来的两倍,所以我们计算相同的哈希值,但
使用不同的表长度计算余数)*/
对于(int i=0;i
哈希表是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;}
}
公共静态类LN{
公共地图。入口;
下一步是公共图书馆;
公共LN(地图入口e、LN)
{entry=e;next=n;}
}
我的类中有一个iterable,但它不允许map[I].entry.entries()

公共可编辑项(){
返回新的Iterable(){
公共迭代器迭代器(){
返回新的MapEntryInterator();
}
};
}

我对如何将公共区域的规模扩大一倍感到非常困惑map;

当哈希表太满时,
HashMap
已经调整了自身大小。您不必调整它的大小。

当哈希表太满时,
HashMap
已经调整了自身大小。您不必调整它的大小。

您的代码将不会编译。如果您想将映射初始化为两倍大小,则更容易his(假设
map
也是
map
):

现在,如前所述,您不需要调整HashMap的大小。它已经做到了。从:

HashMap实例有两个影响其性能的参数: 初始容量和负载系数。容量是 哈希表中的bucket,初始容量只是 创建哈希表时的容量。加载因子为 度量哈希表在返回之前允许达到的完整程度 容量会自动增加。当 哈希表超出了加载因子和当前值的乘积 容量,哈希表被重新格式化(即内部数据 结构)以使哈希表具有大约两个 桶的数量


您的代码将无法编译。如果要将映射初始化为两倍大小,则更容易执行此操作(假定
map
也是
map
):

现在,如前所述,您不需要调整HashMap的大小。它已经做到了。从:

HashMap实例有两个影响其性能的参数: 初始容量和负载系数。容量是 哈希表中的bucket,初始容量只是 创建哈希表时的容量。加载因子为 度量哈希表在返回之前允许达到的完整程度 容量会自动增加。当 哈希表超出了加载因子和当前值的乘积 容量,哈希表被重新格式化(即内部数据 结构)以使哈希表具有大约两个 桶的数量


我感到困惑。后两个片段表明您正在实现自己的哈希表,但第一个片段表明您正在使用java.util.HashMap。我感到困惑。后两个片段表明您正在实现自己的哈希表,但第一个片段表明您正在使用java.util.HashMap。
public Iterable<Map.Entry<K,V>> entries () {
return new Iterable<Map.Entry<K,V>>() {
  public Iterator<Map.Entry<K,V>> iterator() {
    return new MapEntryIterator();
  }
};
}
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.size()* 2);
  resizedMap.putAll(map);
}
for (K key : map.keySet()){
  V value = map.get(key); //get the value from the map
  //do what you need to do
}