ani*_*nir 5 java hashmap java-8
我在Java HashMap 源代码中找到了以下代码:
/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;
/**
* The bin count threshold for untreeifying a (split) bin during a
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
*/
static final int UNTREEIFY_THRESHOLD = 6;
/**
* The smallest table capacity for which bins may be treeified.
* (Otherwise the table is resized if too many nodes in a bin.)
* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
* between resizing and treeification thresholds.
*/
static final int MIN_TREEIFY_CAPACITY = 64;
Run Code Online (Sandbox Code Playgroud)
我的疑问是这样的:
Q0。这三个变量如何共同决定树化和非树化?
然而,上述问题可能是抽象的,因为可能涉及许多微妙的事实,需要在答案中解决才能使其整体完整。由于这些是微妙的事实并且可能在答案中被忽略,我试图在下面的三个单独的问题中列出它们。您可以回答 Q0 或最好回答 Q1、Q2 和 Q3。
我的理解如下:
我们从冲突解决的链表开始。一旦我们将要散列的键插入到已经包含 8 (
TREEIFY_THRESHOLD) 个键的桶中,并且如果MIN_TREEIFY_CAPACITYHashMap中已经有 64 个 ( ) 键,则该桶的链表将转换为平衡树。如果我们从平衡树中包含 6 个键的桶中删除一个键,那么它会被转换回链表。
一季度。以上理解正确吗?
Q2。我不明白为什么
UNTREEIFY_THRESHOLD最多应为 6 以在去除下进行收缩检测。
Q3。我不明白为什么
MIN_TREEIFY_CAPACITY应该至少4 * TREEIFY_THRESHOLD避免调整大小和树化阈值之间的冲突。
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |