HashMap
内部使用Node<K, V>
array
VS内部使用Hashtable
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;
}
Hashtable 正在使用 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
有什么具体原因吗?
两者都使用接口Map.Entry
。 HashTable
,它位于HashMap
之前,提供了它的私有类实现,该实现只能在HashTable
类内部访问。因此,不可能在HashMap
中使用它.