Skip to content
Permalink
Browse files

8046362: IdentityHashMap.hash comments should be clarified

Reviewed-by: aph, martin, rriggs
  • Loading branch information
Stuart Marks
Stuart Marks committed Feb 13, 2020
1 parent 30707f8 commit 5b4d80d106d7bfebd76d82084422981d10ce84fb
Showing with 11 additions and 9 deletions.
  1. +11 −9 src/java.base/share/classes/java/util/IdentityHashMap.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -115,17 +115,19 @@
* exception for its correctness: <i>fail-fast iterators should be used only
* to detect bugs.</i>
*
* <p>Implementation note: This is a simple <i>linear-probe</i> hash table,
* as described for example in texts by Sedgewick and Knuth. The array
* alternates holding keys and values. (This has better locality for large
* tables than does using separate arrays.) For many JRE implementations
* and operation mixes, this class will yield better performance than
* {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).
*
* <p>This class is a member of the
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
*
* @implNote
* <p>This is a simple <i>linear-probe</i> hash table,
* as described for example in texts by Sedgewick and Knuth. The array
* contains alternating keys and values, with keys at even indexes and values
* at odd indexes. (This arrangement has better locality for large
* tables than does using separate arrays.) For many Java implementations
* and operation mixes, this class will yield better performance than
* {@link HashMap}, which uses <i>chaining</i> rather than linear-probing.
*
* @see System#identityHashCode(Object)
* @see Object#hashCode()
* @see Collection
@@ -293,7 +295,7 @@ public boolean isEmpty() {
*/
private static int hash(Object x, int length) {
int h = System.identityHashCode(x);
// Multiply by -127, and left-shift to use least bit as part of hash
// Multiply by -254 to use the hash LSB and to ensure index is even
return ((h << 1) - (h << 8)) & (length - 1);
}

0 comments on commit 5b4d80d

Please sign in to comment.