Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c0241aa

Browse files
doc update
1 parent 6243818 commit c0241aa

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

‎README.md‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@ _Reference_: https://dzone.com/articles/java-8-hashmaps-keys-and-the-comparable-
66
_Reference_: https://yermilov.github.io/blog/2017/02/24/tiebreaker-regarding-java-hashmap-treenode-and-tiebreakorder/
77

88
# 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
1614
same bucket, one is considered bigger and goes to the right. If hashes
1715
are equal (as in our case), `HashMap` hopes that the keys are `Comparable`,
1816
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.
2218

2319
The tree implementation inside the `HashMap` is a `Red-Black` tree, which
2420
means it will always be balanced.
2521

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
2723
entry in the tree,
2824
first it checks whether the current and the new values are easily
2925
comparable (`Comparable` interface) or not. In the latter case, it has

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /