英文:
How is Map.Entry used in HashMap implementation?
问题
-
Map.Entry
is declared as a nested interface within theMap
interface. In the provided code, thestatic
keyword is not used because nested interfaces in Java are implicitlystatic
by nature. So, there's no need to specify it explicitly. -
The
table
variable in theHashMap
class is indeedstatic
. This means that all instances of theHashMap
class share the sametable
. EachHashMap
object doesn't have its distincttable
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 thetable
contains information about the key-value pairs stored in theHashMap
.
英文:
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.
-
According to javadoc,
Map.Entry
is apublic static interface
, aspublic static interface Map.Entry<K,V>
. Why isstatic
missing in aboveinterface Entry<K, V>
? Does javadoc refer to Oracle jdk only? -
About the
static
keyword, my understanding is that internalMap.Entry
object,table
's element type, doesn't have reference to theHashMap
becauseNode
is astatic
class. Is thetable
variablestatic
? If thetable
isstatic
, then allHashMap
class objects will share the sametable
; and this doesn't sound right. Doesn't eachHashMap
object have distinct memory storage to host their contents?
答案1
得分: 1
Here is the translated text:
> 为什么上述的 interface Entry<K, V>
中缺少 static
?
来自 JLS:
> 9.5. 成员类型声明
>> 接口中的成员类型声明在隐式情况下是 public
和 static
的。允许多余地指定这两个修饰符中的一个或两个。
> 关于 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
对象都有不同的内存存储来存储它们的内容吗?
是的,这就是像 table
、entrySet
、keySet
和 values
(后两者在超类中)这些内容存储的地方。
英文:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论