gpt4 book ai didi

java - "power of 2"在 java.util.HashMap 实现中的意义

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:13 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Java HashMap Default Initial Capacity

我正在阅读 java.util.HashMap 中 HashMap 的实现。初始容量、最大容量等都是2的幂。

从 java.util.HashMap 复制的部分声明

/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;


/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;


/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry[] table;

评论表明尺寸必须是 2 的幂。为什么二的幂如此重要?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com