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
是否有任何具体原因?
答案 0 :(得分:0)
两者都在使用界面Map.Entry
。 HashTable
之前的HashMap
提供了它的私有类实现,只能在HashTable
类中访问。因此,在HashMap
中使用它是不可能的。