JDK8 source code parsing - HashMap (b)

  In the last JDK8 source code parsing - HashMap (a) a blog about important points HashMap already talked about it, and I'll talk about some of the content in this blog today, and I will put some of my do not understand the question thrown out, I would like to see this blog troubled great God help answer my question, so I understand the why and wherefore come. Progress with each other and grow with each other. HashMap change from jdk7 to jdk8 large version, a new node is added 2. added using a red-black tree at the end of the list.

  1. HashMap capacity size evaluation method

// 返回2的幂次
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

   (1) why there needs int n = cap - 1 this happen?

            First, we need to understand the role of this method is to obtain the most recent value of the input power of 2 size capacity. Suppose you pass over the parameter cap 16 is then, after the following operation is obtained out of 32, instead of 16. So there need to minus one. In order to receive this value is the smallest power of 2 value.

    (2) to the left of here and is 31 = 1 + 2 + 4 + 8 + 16, 31 Why is it here?

             Here capacity type int size, int size is 32 bits, the maximum value of the Integer 2 <sup> 31 </ sup> -1. So moving a median of 31. If there is a cap diminished assigned to n, then the result will exceed this range. Note that the maximum capacity of the HashMap MAXIMUM_CAPACITY = 1 << 30; 30 here, because if this is probably the left, then 31, the worst result of the above calculated value is 1 <<< 32, then more than int the range of the highest numbers of bits are reserved for the sign bit.

  2. Quick failure mechanism (fail-fast)

      HashMap long as the results changes will be called fail-fast mechanism, in addition to iterator's own remove method, the iterator will throw a ConcurrentModificationException. There modCount field the HashMap configuration change recording frequency when performing iterations determines whether the field has changed. If the value is not the same exception is thrown.

  3. Features red-black tree

    (1) node red or black.

    (2) the root is black.

    Child nodes (3) red node are black.

    (4) each leaf (NIL) is black.  

    (5) from any node to all leaves of each path contains the same number of black nodes.

Note: Red child node node can not be red, you must be black. Black child node node can be red also black.

 

Throws question:

    1.HashMap iterator is how to get the value of? For example keySet () method as shown below, I do not know how to achieve. There are also ValueSet HashMap () and the like. If anyone knows the god principle, please my coaching. :)

 

Guess you like

Origin www.cnblogs.com/javJoker/p/12096082.html