Java源码 HashMap.roundUpToPowerOf2原理

int rounded = number >= MAXIMUM_CAPACITY ? MAXIMUM_CAPACITY
 : (rounded = Integer.highestOneBit(number)) != 0 ? 
 (Integer.bitCount(number) > 1) ? rounded << 1 : rounded
                                        : 1;

分析rounded求解过程:

Created with Raphaël 2.1.0开始number >= 2^302^30返回rounded = Integer.highestOneBit(number)rounded !=0 Integer.bitCount(number) > 1rounded << 1 rounded1yesnoyesnoyesno

流程图

Created with Raphaël 2.1.0开始number >= 2^302^30返回rounded,求最接近的r=2^nrounded !=0 number >rnumber>r 则两倍之本来就是2^n的值得round1yesnoyesnoyesno

流程图分析

posted @ 2016-12-18 14:56  一杯半盏  阅读(552)  评论(0编辑  收藏  举报