Home > Back-end >  About the HashMap source
About the HashMap source

Time:04-10

Final V putVal (int hash, K key, V value, Boolean onlyIfAbsent,
Boolean evict) {
Node [] TAB. Node p; Int n, I;
If ((TAB=table)==null | | (n=TAB. Length)==0)
N=(TAB=the resize ().) length;
If ((p=TAB [I=(n - 1) & amp; hash])==null)
TAB [I]=newNode (hash, key, value, null);


A HashMap putVal method, why a new local variable Node< alone; K, V> [] TAB, the TAB=table. Direct operation table

CodePudding user response:

Is the capacity you need a new one

CodePudding user response:

Table is a member variable, TAB=table, the resize () method returns the new Node [] is change after the member variables table.
Reference the address is the same.
Node [] TAB. Node p; Int n, I;
If ((TAB=table)==null | | (n=TAB. Length)==0)
N=(TAB=the resize ().) length;
If ((p=TAB [I=(n - 1) & amp; hash])==null)
TAB [I]=newNode (hash, key, value, null);
The else {
Node e; K K;
If (p.h ash==hash & amp; &
(k=p.k ey)==key | | (key!=null & amp; & Key. Equals (k))))
E=p;
Is a bit like e and p, e and p are just different objects, is a shallow copy of the p, e reference address is the same. E, p will change accordingly. Why again a new e. direct operation p. Around people dizzy.
  • Related