Java Reference ยท Text, Formatting and Regex
Java Objects.hash() method
Reference for Java Objects.hash() and Objects.hashCode(): combine fields into a hash code for hashCode(), null handling, and the performance cost.
Objects.hash(Object... values) computes one int hash code from any number of values. Its main job is to make hashCode() implementations short: instead of combining fields by hand with multiplications and additions, you list the fields that take part in equals().
null values are allowed and count as 0. The algorithm is the same one Arrays.hashCode(Object[]) and List.hashCode() use: start with 1, and for each element multiply the running result by 31 and add the element's hash. Because that formula is documented, the same fields always give the same result on every JVM, as long as the fields' own hashCode() values are stable.
@Override public int hashCode() { return Objects.hash(name, age); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public static int hash(Object... values) public static int hashCode(Object o)
| Name | Description |
|---|---|
values |
The values to combine, in order. May contain null. |
o |
For hashCode: the object to hash, or null. |
Returns: for hash, a combined hash code (1 for no values, 0 if the array itself is null). For hashCode, o.hashCode(), or 0 for null.
Throws: nothing.
Usage notes
- Match
equals(). Use exactly the fields thatequals()compares. Equal objects must have equal hash codes; unequal objects may share a code. - Order matters.
Objects.hash(a, b)andObjects.hash(b, a)usually differ. - It allocates. Every call creates a varargs array and boxes primitives. That is negligible for most code, but for hot paths (keys used millions of times per second) write the multiplication by hand, or cache the hash in an immutable object.
- One argument is not
hashCode.Objects.hash(x)returns31 + hash(x), notx.hashCode(). When you only have one field, callObjects.hashCode(x). - Mutable fields are dangerous. If a field used in the hash changes while the object is in a
HashMaporHashSet, the object can no longer be found. Hash immutable fields. - Arrays inside fields: pass them through
Arrays.hashCode(ordeepHashCode) first; an array's ownhashCode()is identity-based. - Records and enums do not need this: a record generates
hashCode()from its components, and an enum uses identity. - Do not persist hash codes.
String.hashCode()is specified, but the hash of many other classes (and any defaultObject.hashCode()) can vary between runs.
Related
Object, equals() and hashCode()- Tutorials: How HashMap works, The Object class