how does entrySet in hashmap get created and modified?

maki XIE :
/**
 * Holds cached entrySet(). Note that AbstractMap fields are used
 * for keySet() and values().
 */
transient Set<Map.Entry<K,V>> entrySet;

I find a strange thing in java Hashmap. I find no code to create an instance of and manipulate entrySet in hashmap source code and AbstractMap code. But when I put key-value in hashmap, the entrySet gets the new value. It's strange.

So my question is:

how does entrySet gets the new value, and when does it get its instance?

To be more clear, I added my debug code in IDE and find before my first call of this set, it is already not null and filled with elements.

enter image description here

map

Tom Hawtin - tackline :

Map.entrySet will just give you a view of the Map as a Set<Map.Entry<,>>. It doesn't copy the data structure. List.subList is the same. Arrays.asList should be an easy example to understand.

In Java inner classes have two (or more) thiss (assuming they are not local classes in a static context). In Python, by convention, self is used for this. (I haven't done that much Python.) So any methods of inner class would be hiding the outer self. You would have to use different names, and of course be explicit every time you use them. In Java, if you are not explicit, it'll pick the this that fits (which can be weird in corner cases).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19091&siteId=1