-1

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?

user1803551
  • 12,965
  • 5
  • 47
  • 74
sofabc
  • 3
  • 1
  • 1. All interfaces are static. 2. The `table` variable is not static, as your own code extract shows. The `Map.Entry` objects only need their key and value references, not a reference to the `HashMap`. Your reasoning on this point is unclear. – user207421 Jul 29 '20 at 04:52

1 Answers1

1

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.

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Thanks @user1803551 1. Since the table is a nonstatic member, each table object contains an implicit reference to its outer class, the HashMap. 2. table's element is Node. Since Node is static class member, each table's element does NOT contain a reference to its outer class, the HashMap. If all Node objects are static, they are essentially shared by all HashMap objects. This is strange because Node object is mutable (via Map.Entry's setValue() method). If one HashMap object modifies a Node object, another HashMap object can see the change. Doesn't it violate encapsulation principle? – sofabc Jul 30 '20 at 16:03
  • @sofabc *If all Node objects are static, they are essentially shared by all HashMap objects* No. The class is `static`. `static` does not apply to the instances of that class. *If one HashMap object modifies a Node object, another HashMap object can see the change.* No because of the above. – user1803551 Aug 02 '20 at 14:29
  • @ user1803551, you hit it right on the head. Node class is static; and Node objects are nonstatic. The static class type still gives Node objects some specialty. Each Node object doesn't implicitly contain a reference to its outer class, the HashMap. Thank you so much for explaining. – sofabc Aug 03 '20 at 04:56