Map中提供的一个算法(提供一个值,返回大于等于该值的最接近的2的指数幂)

来源:互联网 发布:mac版360浏览器 编辑:程序博客网 时间:2024/05/16 08:01

Map中提供的一个算法(提供一个值,返回大于等于该值的最接近的2的指数幂)
eg:(例:5–>8;9–>16,16–>16)

static final int MAXIMUM_CAPACITY = 1 << 30;

/**
* 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;
}

阅读全文
0 0
原创粉丝点击