JDK 10 [New Enhancement] - Consistently use primitive hashCode() variants in Arrays.hashCode() for primitive arrays
Christoph Dreis
christoph.dreis at freenet.de
Wed Mar 29 18:33:59 UTC 2017
Hey,
I just noticed that the Arrays.hashCode() variants for arrays of primitive
types could use the methods in Float, Boolean etc. instead of duplicating
the code. Please find the patch below.
What do you think? I'd be happy if this is picked up for JDK 10.
Cheers,
Christoph
=========== PATCH ======================
diff --git a/src/java.base/share/classes/java/util/Arrays.java
b/src/java.base/share/classes/java/util/Arrays.java
--- a/src/java.base/share/classes/java/util/Arrays.java
+++ b/src/java.base/share/classes/java/util/Arrays.java
@@ -4456,8 +4456,7 @@
int result = 1;
for (long element : a) {
- int elementHash = (int)(element ^ (element >>> 32));
- result = 31 * result + elementHash;
+ result = 31 * result + Long.hashCode(element);
}
return result;
@@ -4593,7 +4592,7 @@
int result = 1;
for (boolean element : a)
- result = 31 * result + (element ? 1231 : 1237);
+ result = 31 * result + Boolean.hashCode(element);
return result;
}
@@ -4620,7 +4619,7 @@
int result = 1;
for (float element : a)
- result = 31 * result + Float.floatToIntBits(element);
+ result = 31 * result + Float.hashCode(element);
return result;
}
@@ -4647,8 +4646,7 @@
int result = 1;
for (double element : a) {
- long bits = Double.doubleToLongBits(element);
- result = 31 * result + (int)(bits ^ (bits >>> 32));
+ result = 31 * result + Double.hashCode(element);
}
return result;
}
More information about the core-libs-dev
mailing list