Map.Entry下一个方法?

时间:2013-05-15 17:08:47

标签: java hashmap openjdk

我正在查看JDK 7的源代码 - 特别是WeakHashMap.java,其中广泛使用了这个:

Entry<K,V> p = prev;
Entry<K,V> next = p.next;

但接下来不是在Map.Entry上定义的方法(据我所见?http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html

从那时起对这种方法的调用在哪里?

3 个答案:

答案 0 :(得分:5)

WeakHashMap并未指代Map.Entry,而是指Map.EntryEntry接口的internal implementation类,名为next。该类有一个名为{{1}}的字段,可以访问。

答案 1 :(得分:0)

next不是一个功能。它是直接访问的类的成员变量。通常你会看到这个(在OO代码中)

public class Foo  
{  
    String bar;  

    public String getBar()  
    {   
        return this.bar;
    }  
}  

您看到的.next语法通常被称为直接成员访问,通常不受欢迎。

答案 2 :(得分:0)

它是WeakHashMap内部的一个内部类,它包含对Entry next的引用:

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

    Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) {
    super(key, queue);
    this.value = value;
    this.hash  = hash;
    this.next  = next;

    // omit others...
}

查看第682行的源代码:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/WeakHashMap.java#WeakHashMap.Entry

这对HashMap来说是一回事。它有类似的内部类:

static class Entry<K, V> implements Map.Entry<K, V>