Skip to content

8352171: Arrays.hashCode for sub-range of byte array API addition #24128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/java.base/share/classes/java/util/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -4428,6 +4428,36 @@ public static int hashCode(byte[] a) {
return ArraysSupport.hashCode(a, 0, a.length, 1);
}

/**
* Computes a hash code for a specified sub-range of the given {@code byte} array.
* The sub-range is defined by the indices from {@code fromIndex} (inclusive)
* to {@code toIndex} (exclusive).
*
* <p>The computed hash code is based on the content of the sub-range. Specifically,
* it is equivalent to the hash code that would be produced by a {@code List} of
* {@code Byte} objects representing the elements in the sub-range, in the same order.
* Thus, if two sub-ranges (within the same array) are equal as determined by
* {@link #equals(byte[], int, int, byte[], int, int)}, they will yield the same hash code.
* If the array {@code a} is {@code null}, this method returns {@code 0}.
*
* @param a the array from which to compute the sub-range hash code
* @param fromIndex the starting index (inclusive) of the sub-range
* @param toIndex the ending index (exclusive) of the sub-range
* @return a content-based hash code for the specified sub-range of the array,
* or {@code 0} if {@code a} is {@code null}
* @throws ArrayIndexOutOfBoundsException if {@code fromIndex} or {@code toIndex}
* are not valid indices for the array {@code a}
* @throws IllegalArgumentException if {@code fromIndex} is greater than {@code toIndex}
* @since 25
*/
public static int hashCode(byte[] a, int fromIndex, int toIndex) {
if (a == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The a == null special check is dubious - a == null has a meaning if we are hashing a whole byte[] object, but with a specific fromIndex and toIndex it just doesn't work.

That said, we might consider accepting the initial hash too - For example, we have two byte[] arrays and we want a concatenated hash. The initial hash allows us to compute such a hash from two arrays easily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the other public APIs for hashcode include an initial value argument.
The caller can easily combine the hashcode values themselves as needed.

return 0;
}
rangeCheck(a.length, fromIndex, toIndex);
return ArraysSupport.hashCode(a, fromIndex, toIndex - fromIndex, 1);
}

/**
* Returns a hash code based on the contents of the specified array.
* For any two {@code boolean} arrays {@code a} and {@code b}
Expand Down