← All writing

Java

Objects.hashCode() vs Objects.hash() in Java

Compare Objects.hashCode and Objects.hash, including their inputs and hashing behavior.

1 view Copy this article’s link to your clipboard.
Feature / Property Objects.hashCode(Object o) Objects.hash(Object... values)
Method type Static utility method Static utility method
Parameters Single object (nullable) Varargs: 0 or more objects (nullable)
Main purpose Safe way to get hashCode of single object Convenient way to compute combined hash for multiple fields
What it does Returns 0 if input is null<br>Otherwise → calls o.hashCode() Computes combined hash code of all arguments (null → 0)
Null handling Returns 0 (null-safe) Treats each null argument as 0 (null-safe)
Typical usage return Objects.hashCode(name); return Objects.hash(name, age, email);
When you override hashCode() Use when class has only one significant field Most common choice when class has multiple fields
Performance Very fast (basically one method call + null check) Slightly slower (due to array creation + loop)<br>But difference usually negligible
Introduced in Java 7 Java 7
Recommendation Good for single-field classes Preferred for most real-world hashCode() implementations
More articles →