diff --git a/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java b/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java index b38d237f212..b7d88bfc3db 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java +++ b/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java @@ -174,8 +174,14 @@ public int hashCode() { if (h == 0) { byte[] bytes = this.bytes; int length = this.length; - for (int i = 0; i < length; i++) { - h = h * 31 + bytes[i]; + // If the array is filled, use the underlying JDK hash functionality. + // Starting with JDK 21, the underlying implementation is vectorized. + if (bytes.length == length) { + h = Arrays.hashCode(bytes); + } else { + for (int i = 0; i < length; i++) { + h = h * 31 + bytes[i]; + } } this.hash = h; }