请在 java 中分享一些关于 rehash 方法的见解?

Please share some insights on rehash method in java?

我正在寻找关于 hashtable/hash-map 数据结构的更好见解。

通过 api 我可以看出内部条目 class 被称为桶。如有不妥请指正

请寻找以下方法:-

  public synchronized V put(K key, V value) {
    // Make sure the value is not null
    if (value == null) {
        throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    Entry tab[] = table;
    int hash = hash(key);
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            V old = e.value;
            e.value = value;
            return old;
        }
    }

    modCount++;
    if (count >= threshold) {
        // Rehash the table if the threshold is exceeded
        rehash();

        tab = table;
        hash = hash(key);
        index = (hash & 0x7FFFFFFF) % tab.length;
    }

    // Creates the new entry.
    Entry<K,V> e = tab[index];  <-------are we assigining null to this entry?
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
    return null;
}

通过下面一行代码

Entry<K,V> e = tab[index];

我可以假设我们正在将 null 分配给这个新的条目对象;也请在这里纠正我。

所以我的另一个问题是:-

为什么我们不直接这样做

Entry<K,V> e = null 
 instead  of 
Entry<K,V> e = tab[index];

请在下面找到调试的屏幕截图:-

请分享您对此的宝贵见解。

Entry<K,V> 是一个实例,可以表示 linked 列表中的 link。请注意,next 成员指的是列表中的下一个条目。

一个存储桶包含一个 linked 条目列表,这些条目映射到同一索引。

Entry<K,V> e = tab[index] 将 return 仅当该索引中还没有存储条目时才为 null。否则它将 return 该存储桶的 linked 列表中的第一个条目。

tab[index] = new Entry<>(hash, key, value, e); 创建一个新条目并将其存储为存储桶中的第一个条目。先前的第一个 Entry 被传递给 Entry 构造函数,以便成为列表中的下一个(第二个)Entry。