HashMap 内部使用 Node<K、V> 数组与 Hashtable 内部使用 Map.Entry<K、V> 数组,为什么会有这种内部差异?



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.EntryHashTable ,它位于HashMap之前,提供了它的私有类实现,该实现只能在HashTable类内部访问。因此,不可能在HashMap中使用它.

最新更新