gpt4 book ai didi

java - HashMap 内部使用 Node 数组,而 Hashtable 内部使用 Map.Entry 数组,为什么会出现这种内部差异?

转载 作者:行者123 更新时间:2023-12-02 19:55:34 25 4
gpt4 key购买 nike

HashMap内部使用Node<K, V> arrayHashtable内部使用Map.Entry<K, V> array ,为什么会出现这种内部差异:

HashMap 使用 Node 内部类和 Map.Entry 实现。

static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;

Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}

哈希表正在使用 Map.Entry。

private static class Entry<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Entry<K,V> next;

protected Entry(int hash, K key, V value, Entry<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}

两者的接缝相同但又不同。
是否有任何特定原因使用HashMap正在使用Node<K,V> array而不是Map.Entry<K,V> array

最佳答案

两者都使用接口(interface)Map.EntryHashTable 位于 HashMap 之前,提供了它的私有(private)类实现,该实现只能在 HashTable 类内部访问。因此,不可能在 HashMap 中使用它。

关于java - HashMap 内部使用 Node<K, V> 数组,而 Hashtable 内部使用 Map.Entry<K, V> 数组,为什么会出现这种内部差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27294761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com