I find a very magical thing, simple code, as shown below:
public class Demo{ public static void main(String[] args){ HashMap<String,String> map = new HashMap<String,String>(); map.put("a", "aa"); System.out.println("end"); } }
after calling
HashMap<String,String> map = new HashMap<String,String>();
the entrySet field variable is not null, that is, it has been initialized.
Then this is my first question when the entrySet was initialized? it seemed that the corresponding code should be in the HashMap construct, but below is the source code of this constructor
public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR;
there seemed to be no code that initializes the entrySet .
and the thing goes on. after calling
map.put("a","aa")
contents of the table of field variables and entrySet, as shown below.
Then this is my second problem: add this value to entrySet? It seemed like it should be a put method. and below is the put method.
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); }
it calls the putVal method, and below is some putVal code
final V putVal(...) { .... tab[i] = newNode(hash, key, value, null); .... ++modCount;//after invoke this the entrySet is still empty if (++size > threshold) resize();//this has not been executed afterNodeInsertion(evict);//I debug several times, sometimes before invoke this the entrySet has an Element and sometimes return null; }
after calling
++modCount;
entrySet is empty and before the call
afterNodeInsertion(evict);
entrySet has an element. but it seemed that the code between the two lines had nothing with entrySet. I think maybe there are several threads that control the entrySet, then I write a small tool with jvm_ti to print threadID that calls the class below the java.util package and finds only one thread.
Then what do I miss? Is there a problem while debugging? I would like for me to clearly describe my problem and everything would be valuable.
add: my java version is 1.8.0_77 and eclipse version is 4.6.1 and 4.5.1