Arrays.hashCode for sub-range of byte array API addition

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Component/s: core-libs
    • None

      Summary

      Add a new method public static int hashCodeOfRange(byte[] a, int fromIndex, int toIndex) to the java.util.Arrays class to support computing a content-based hash code for a specific sub-range of a byte array.

      Problem

      Currently, java.util.Arrays only provides hashCode(byte[]) which operates on the entire array. When developers need to compute a hash code for a sub-range (slice) of a byte array, they are forced to either:

      1. Copy the range into a new array using Arrays.copyOfRange.

      2. Manually implement the hash loop.

      Solution

      Introduce a new method hashCodeOfRange for byte arrays: public static int hashCodeOfRange(byte[] a, int fromIndex, int toIndex)

      • Naming: The name hashCodeOfRange is chosen to be consistent with the existing naming pattern in Arrays (specifically copyOfRange). It clearly distinguishes the method from the standard hashCode(byte[]) overload and explicitly indicates that it operates on a sub-range.

      • Validation: If a is null, this method treats the array as having length 0 for range checking and returns 0 only for the empty range [0,0). Throws ArrayIndexOutOfBoundsException if fromIndex < 0 or toIndex is greater than the array length.

      Specification

      diff --git a/src/java.base/share/classes/java/util/Arrays.java b/src/java.base/share/classes/java/util/Arrays.java
      index 1bba844f791..aa941618fb3 100644
      --- a/src/java.base/share/classes/java/util/Arrays.java
      +++ b/src/java.base/share/classes/java/util/Arrays.java
      @@ -4428,6 +4428,45 @@ public static int hashCode(byte[] a) {
               return ArraysSupport.hashCode(a, 0, a.length, 1);
           }
       
      +    /**
      +     * Returns a hash code based on the contents of the specified range of the
      +     * specified {@code byte} array.
      +     * For any two {@code byte} arrays {@code a} and {@code b} and ranges
      +     * {@code aFromIndex}..{@code aToIndex} and {@code bFromIndex}..{@code bToIndex}
      +     * such that
      +     * {@link #equals(byte[], int, int, byte[], int, int)
      +     * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex)}
      +     * returns {@code true}, it is also the case that
      +     * {@code hashCodeOfRange(a, aFromIndex, aToIndex) ==
      +     * hashCodeOfRange(b, bFromIndex, bToIndex)}.
      +     *
      +     * <p>The value returned by this method is the same value that would be
      +     * obtained by invoking the {@link List#hashCode() hashCode}
      +     * method on a {@link List} containing a sequence of {@link Byte}
      +     * instances representing the elements of the specified range in the same
      +     * order. If {@code a} is {@code null}, this method treats the array as
      +     * having length {@code 0} for range checking and returns {@code 0} only
      +     * for the empty range {@code [0,0)}.
      +     *
      +     * @param a the array from which to compute the range hash code
      +     * @param fromIndex the initial index of the range to be hashed, inclusive
      +     * @param toIndex the final index of the range to be hashed, exclusive
      +     * @return a content-based hash code for the specified range of the array,
      +     *         or {@code 0} if {@code a} is {@code null} and the range is empty
      +     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
      +     *         {@code toIndex} is greater than the array length
      +     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
      +     * @since 27
      +     */
      +    public static int hashCodeOfRange(byte[] a, int fromIndex, int toIndex) {
      +        if (a == null) {
      +            rangeCheck(0, fromIndex, toIndex);
      +            return 0;
      +        }
      +        rangeCheck(a.length, fromIndex, toIndex);
      +        return ArraysSupport.hashCode(a, fromIndex, toIndex - fromIndex, 1);
      +    }
      +
      

            Assignee:
            Zihao Lin
            Reporter:
            Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: