< prev index next >

src/java.base/share/classes/java/util/Arrays.java

Print this page
8202398: Optimize Arrays.deepHashCode
Reviewed-by: psandoz

*** 4721,4758 **** return 0; int result = 1; for (Object element : a) { ! int elementHash = 0; ! if (element instanceof Object[]) ! elementHash = deepHashCode((Object[]) element); ! else if (element instanceof byte[]) ! elementHash = hashCode((byte[]) element); ! else if (element instanceof short[]) ! elementHash = hashCode((short[]) element); ! else if (element instanceof int[]) ! elementHash = hashCode((int[]) element); ! else if (element instanceof long[]) ! elementHash = hashCode((long[]) element); ! else if (element instanceof char[]) ! elementHash = hashCode((char[]) element); ! else if (element instanceof float[]) ! elementHash = hashCode((float[]) element); ! else if (element instanceof double[]) ! elementHash = hashCode((double[]) element); ! else if (element instanceof boolean[]) ! elementHash = hashCode((boolean[]) element); ! else if (element != null) elementHash = element.hashCode(); result = 31 * result + elementHash; } return result; } /** * Returns {@code true} if the two specified arrays are <i>deeply * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])} * method, this method is appropriate for use with nested arrays of * arbitrary depth. --- 4721,4761 ---- return 0; int result = 1; for (Object element : a) { ! final int elementHash; ! final Class<?> cl; ! if (element == null) ! elementHash = 0; ! else if ((cl = element.getClass().getComponentType()) == null) elementHash = element.hashCode(); + else if (element instanceof Object[]) + elementHash = deepHashCode((Object[]) element); + else + elementHash = primitiveArrayHashCode(element, cl); result = 31 * result + elementHash; } return result; } + private static int primitiveArrayHashCode(Object a, Class<?> cl) { + return + (cl == byte.class) ? hashCode((byte[]) a) : + (cl == int.class) ? hashCode((int[]) a) : + (cl == long.class) ? hashCode((long[]) a) : + (cl == char.class) ? hashCode((char[]) a) : + (cl == short.class) ? hashCode((short[]) a) : + (cl == boolean.class) ? hashCode((boolean[]) a) : + (cl == double.class) ? hashCode((double[]) a) : + // If new primitive types are ever added, this method must be + // expanded or we will fail here with ClassCastException. + hashCode((float[]) a); + } + /** * Returns {@code true} if the two specified arrays are <i>deeply * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])} * method, this method is appropriate for use with nested arrays of * arbitrary depth.
< prev index next >