1

i've seen the answer of How do HashMap.values() and HashMap.keySet() return values and keys? and Java: private inner class synthesized constructors i think this question is not duplicate...

i still don't know how keySet() of HashMap works the point confused me is: it's null before call the method keySet(),but once invoked,the value "keySet" will be associated with the map.

    Map map = new HashMap();
    map.put(1, 1);  //null
    map.keySet();   //[1]
    map.put(2, 2);  //[1,2]
    map.remove(2);  //[1]

so i'm strange about how it works....

the inner class KeySet can access the Outter class "HashMap" variables,but in class AbstractMap,there is only a Set keySet = null;,the keys and values are stored in the Entry[] table; together, i still cant find a method like

    for(Entry e : table) {
        keySet.put(e.getKey());
    }

to make a refer between only map keys and keySet

Community
  • 1
  • 1
Just.Joke
  • 25
  • 1
  • 5
  • 2
    It's an *inner class*. Java automatically generators constructors to pass the instance of the outer class to the inner class. – Erwin Bolwidt Aug 04 '15 at 01:47
  • @ElliottFrisch yes,"Returns a Set view of the keys contained in this map",but i get nothing about what i asked – Just.Joke Aug 04 '15 at 04:23
  • @ErwinBolwidt eh..but it has two~one has two params (java.util.HashMap,java.util.HashMap$1)...what's HashMap$1 and can i build a demo inner class like this,has two or more constroctors implicitly – Just.Joke Aug 04 '15 at 04:34
  • where's ElliottFrisch's comment? it's disappeared? and "@" really functioned? – Just.Joke Aug 04 '15 at 04:36
  • Please read this question and its accepted answer, it explains why there are two constructors: https://stackoverflow.com/questions/15199292/java-private-inner-class-synthesized-constructors – Erwin Bolwidt Aug 04 '15 at 05:02
  • @ErwinBolwidt thanks! but how keySet() get map keys?i mean i cant find the code how it work,it just return (ks != null ? ks : (keySet = new KeySet())),nothing else,when debug step by step, i can't get any other information when "new KeySet()" ,i can only know after it's newed,then it has the keys... – Just.Joke Aug 04 '15 at 05:33
  • It gets them from the Map. It's a *inner class*. Inner classes can directly refer to the outer class instance. That's why the Java compiler is passing the outer class instance under the hood to the hidden constructor; but you don't need to know that (it's a hidden implementation detail). You just need to read up on the topic of inner classes. – Erwin Bolwidt Aug 04 '15 at 05:46
  • @ErwinBolwidt yes,it has the access to the outer class,it can for example get the refer of like Entry[] table, and i mean i can't find how keySet() get out of only keys through code, like for(Entry e: table){ keySet.put(e.getKey();) ;} and i have not enough access to disscuss on chat? – Just.Joke Aug 04 '15 at 06:12
  • The KeySet is a view over the map. It doesn't contain a copy of all the keys of the map. Its elements are the keys of the map, which are already stored in the map. So size() returns the size of the map, iterator() returns an iterator that iterates through the entries of the map and returns their keys, contains tests if the key exists in the map, etc. – JB Nizet Aug 04 '15 at 06:50
  • @JBNizet api just say so... but how it realized..when new KeySet(),the iterator() never called,i think iterator is to iterate,not for put the keys into keySet – Just.Joke Aug 04 '15 at 07:03
  • add() and addAll() are not supported by the returned Set, as documented. So the only way to add elements to the set is to add new entries to the map. The Set iterator, since it iterates through the entries of the map, will return the keys of the added entries. – JB Nizet Aug 04 '15 at 08:49
  • @JBNizet yes,and add or remove of the map will take effect on keySet,so there is a reference between keySet and the variable "table" (the Entry table),but i can't find how they associated – Just.Joke Aug 04 '15 at 10:20
  • You were already told that several times: the set is an inner class of HashMap, and thus has an implicit reference to the HashMap instance, and can access its private members. – JB Nizet Aug 04 '15 at 11:58
  • @JBNizet yes~but which private member? i can't find it... – Just.Joke Aug 05 '15 at 00:26
  • @JBNizet oh i got it...just an empty KeySet.but when watch the keySet member in eclipse watch window,it will call the toString method – Just.Joke Aug 05 '15 at 00:53

0 Answers0