Map.Entry 在 HashMap 实现中如何使用?

huangapple go评论87阅读模式
英文:

How is Map.Entry used in HashMap implementation?

问题

  1. Map.Entry is declared as a nested interface within the Map interface. In the provided code, the static keyword is not used because nested interfaces in Java are implicitly static by nature. So, there's no need to specify it explicitly.

  2. The table variable in the HashMap class is indeed static. This means that all instances of the HashMap class share the same table. Each HashMap object doesn't have its distinct table instance. Instead, they all use the same underlying data structure to store key-value pairs. This design is a fundamental part of how hash tables work in Java to efficiently store and retrieve data. Each entry within the table contains information about the key-value pairs stored in the HashMap.

英文:

I have a question of how Map.Entry used in Map implementation like HashMap. Here is the code of how HashMap is implemented in jdk8,

public interface Map<K, V> {
    interface Entry<K, V> {
    	...
    }
}

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {

    static class Node<K,V> implements Map.Entry<K,V> {
        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
	}

	transient Node<K,V>[] table;

}

I have 2 questions.

  1. According to javadoc, Map.Entry is a public static interface, as public static interface Map.Entry<K,V>. Why is static missing in above interface Entry<K, V>? Does javadoc refer to Oracle jdk only?

  2. About the static keyword, my understanding is that internal Map.Entry object, table's element type, doesn't have reference to the HashMap because Node is a static class. Is the table variable static? If the table is static, then all HashMap class objects will share the same table; and this doesn't sound right. Doesn't each HashMap object have distinct memory storage to host their contents?

答案1

得分: 1

Here is the translated text:

> 为什么上述的 interface Entry<K, V> 中缺少 static

来自 JLS

> 9.5. 成员类型声明
>> 接口中的成员类型声明在隐式情况下是 publicstatic 的。允许多余地指定这两个修饰符中的一个或两个。

> 关于 static 关键字,我的理解是,内部的 Map.Entry 对象,即 table 的元素类型,没有引用到 HashMap,因为 Node 是一个 static 类。

它没有引用,因为它不需要。您需要阅读关于哈希映射数据结构的内容,以了解它是如何工作的。如果您想知道一个 Node 可以有一个引用 到地图吗,那么答案是肯定的,如果它通过构造函数或方法传递给它的话。如果您想知道一个 Node 可以访问 包含的地图吗,那么答案是否定的,因为正如您所说,Node 是静态的。如果您尝试执行以下类似的操作,您将收到错误消息:

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

    HashMap<K,V> map = HashMap.this;
}

错误消息是 No enclosing instance of the type HashMap is accessible in scope。如果 Node 不是 static,那么代码将会编译通过。

> table 变量是 static 吗?

不是,它是 transient,这意味着它不会通过 Serializable 接口的协议进行序列化。

> 每个 HashMap 对象都有不同的内存存储来存储它们的内容吗?

是的,这就是像 tableentrySetkeySetvalues(后两者在超类中)这些内容存储的地方。

英文:

> Why is static missing in above interface Entry<K, V>?

From the JLS:

> 9.5. Member Type Declarations
>> A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.

> About the static keyword, my understanding is that internal Map.Entry object, table's element type, doesn't have a reference to the HashMap because Node is a static class.

It doesn't have a reference because it doesn't need one. You will need to read about the hash map data structure to understand how it works. If you're asking if a Node can have a reference to the map, then yes, if it passed to it (through the constructor or a method). If you're asking if a Node can access the enclosing map, then no, because as you said, Node is static. The error you will get if you try to do something like

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

    HashMap<K,V> map = HashMap.this;
}

is No enclosing instance of the type HashMap is accessible in scope. If Node is not static, then the code will compile.

> Is the table variable static?

No, it is transient, which means it will not be serialized through the Serializable interface's protocol.

> Doesn't each HashMap object have distinct memory storage to host their contents?

Yes, that's where contents like table, entrySet, keySet and values (the latter 2 are in the superclass) are stored.

huangapple
  • 本文由 发表于 2020年7月29日 12:03:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63146170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定