Because sometimes it's easier to reason with number of bits, rather than numerical ranges. In the case of HashMap
, however, another reason is that you need to do hash % numberOfBuckets
, the modulo operation is expensive, so to fasten up one can use a power of two, then modulo may be replaced with logical AND - it'll do the same, but works that way only for powers of two. The Arrays
case - may be similar reasons.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…