HashMap内部使用Node <k,v =“”>数组vs Hashtable内部使用Map.Entry <k,v =“”>数组,为什么这个内部差异?</k,> </k,>

时间:2014-12-04 13:02:25

标签: java arrays collections hashmap hashtable

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是否有任何具体原因?

1 个答案:

答案 0 :(得分:0)

两者都在使用界面Map.EntryHashTable之前的HashMap提供了它的私有类实现,只能在HashTable类中访问。因此,在HashMap中使用它是不可能的。