/**
* Returns a power of two size for the given target capacity.
*/
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;
}
源码的解释是:返回一个指定容量大小的2次幂,人话就是说返回比给定参数大的、最接近的2的n次方那个数
即如果传入10,返回2^4 = 16、传入18,返回2^5 = 32,传入34,应该返回2^6 = 64
测试一下
public static void main(String[] args) {
System.out.println("传入10----"+tableSizeFor(10));
System.out.println("传入18----"+tableSizeFor(18));
System.out.println("传入34----"+tableSizeFor(34));
}
static final int tableSizeFor(int cap) {
Integer MAXIMUM_CAPACITY =