@@ -6,24 +6,20 @@ _Reference_: https://dzone.com/articles/java-8-hashmaps-keys-and-the-comparable-
6
6
_ Reference_ : https://yermilov.github.io/blog/2017/02/24/tiebreaker-regarding-java-hashmap-treenode-and-tiebreakorder/
7
7
8
8
# preface
9
- Basically when a bucket becomes too big (currently: ` TREEIFY_THRESHOLD = 8 ` ),
10
- ` HashMap ` dynamically replaces it with an ad-hoc implementation of tree
11
- map. This way rather than having pessimistic ` O(n) ` we get much better
12
- ` O(logn) ` . How does it work? Well, previously entries with conflicting
13
- keys were simply appended to linked list, which later had to be traversed.
14
- Now ` HashMap ` promotes list into binary tree, using hash code as a
15
- branching variable. If two hashes are different but ended up in the
9
+ When a bucket exceeds threshold (` TREEIFY_THRESHOLD = 8 ` ),
10
+ ` HashMap ` dynamically replaces it a tree map. Instead of having
11
+ pessimistic ` O(n) ` we get ` O(logn) ` . Previously entries with conflicting
12
+ keys were appended to linked list. Now ` HashMap ` uses binary tree (hash code as a
13
+ branching variable). If two hashes are different but ended up in the
16
14
same bucket, one is considered bigger and goes to the right. If hashes
17
15
are equal (as in our case), ` HashMap ` hopes that the keys are ` Comparable ` ,
18
16
so that it can establish some order. This is not a requirement of
19
- ` HashMap ` keys, but apparently a good practice. If keys are not
20
- comparable, don't expect any performance improvements in case of heavy
21
- hash collisions.
17
+ ` HashMap ` keys, but apparently a good practice.
22
18
23
19
The tree implementation inside the ` HashMap ` is a ` Red-Black ` tree, which
24
20
means it will always be balanced.
25
21
26
- When the ` HashMap ` implementation tries to find the location of a new
22
+ When the ` HashMap ` implementation tries to find the location of a
27
23
entry in the tree,
28
24
first it checks whether the current and the new values are easily
29
25
comparable (` Comparable ` interface) or not. In the latter case, it has
0 commit comments