In Java, hashCode() returns an int that hash-based collections use to place an object in a bucket. For an Integer, the hash code is simply the number itself, so Integer.valueOf(42).hashCode() is 42 and -7 gives -7.
Since Java 8 there is also a static form, Integer.hashCode(int), which gives the same answer without creating an object. It is handy when you build the hash code of your own class from several int fields.
Available both as an instance method and as a static method of java.lang.Integer. Integer lives in java.lang, so no import is needed.
Syntax
Call the instance form on an Integer, or the static form with an int.
Java
Integer boxed = 42;
int a = boxed.hashCode(); // 42
int b = Integer.hashCode(42); // 42 (static, Java 8+)
int c = Integer.hashCode(-7); // -7
Call
What it does
boxed.hashCode()
The hash code of an Integer object: its value.
Integer.hashCode(value)
The same result for a primitive int. The static form needs Java 8.
Objects.hash(a, b)
Combines several values into one hash code.
Arrays.hashCode(int[] values)
The hash code of a whole int array.
The instance method has existed since Java 1.0, and Integer.hashCode(int) was added in Java 8. Equal Integer objects always return equal hash codes.
Library declarations
Java
// java.lang.Integer
public int hashCode() // instance
public static int hashCode(int value) // static, Java 8 and later
Parameters
Parameter
Description
value (int)
Only for the static form: the number to hash. The instance form takes no parameter.
Return Value
Return Value
Description
int
The hash code, which equals the numeric value of the Integer.
Instance and static agree. Both forms return 42 for the number 42.
Sign is kept. The hash code of -7 is -7, because the method returns the value unchanged.
Zero and the extremes. 0 hashes to 0, and MIN_VALUE hashes to itself, -2147483648.
No calculation. The method does not mix bits; for Integer, the value already works as a hash code.
Example 2 — Build a Hash Code for a Point Class
Combine two int fields into a hash code with Integer.hashCode, and compare with Objects.hash.
Java
import java.util.Arrays;
import java.util.Objects;
public class IntegerHashCodePoint {
static class Point {
final int x;
final int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Point)) {
return false;
}
Point other = (Point) o;
return x == other.x && y == other.y;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + Integer.hashCode(x);
result = 31 * result + Integer.hashCode(y);
return result;
}
}
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println("Point(3, 4): " + p.hashCode());
System.out.println("Objects.hash: " + Objects.hash(3, 4));
System.out.println("Arrays.hashCode: " + Arrays.hashCode(new int[] {3, 4}));
System.out.println("Point(4, 3): " + new Point(4, 3).hashCode());
}
}
Start at 1, multiply by 31. This is the classic recipe: take the running result, multiply by 31, then add the hash code of the next field.
The arithmetic. After x: 31 × 1 + 3 = 34. After y: 31 × 34 + 4 = 1058.
Library helpers agree.Objects.hash(3, 4) and Arrays.hashCode use the same recipe, so they also print 1058.
Order matters. Point(4, 3) gives 31 × 35 + 3 = 1088, a different value from Point(3, 4).
Example 3 — See Hash Codes Keep Duplicates Out of a Set
Add equal Integer objects to a HashSet and see that they collapse into one entry.
Java
import java.util.HashSet;
import java.util.Set;
public class IntegerHashCodeSet {
public static void main(String[] args) {
Integer first = Integer.valueOf(1000);
Integer second = Integer.valueOf(1000);
System.out.println("same object: " + (first == second));
System.out.println("equals: " + first.equals(second));
System.out.println("same hash: " + (first.hashCode() == second.hashCode()));
Set<Integer> numbers = new HashSet<>();
numbers.add(first);
numbers.add(second);
System.out.println("set size: " + numbers.size());
}
}
Output
same object: false
equals: true
same hash: true
set size: 1
How It Works
Two separate objects. 1000 is outside the -128 to 127 cache, so each valueOf call builds a fresh object and the two variables point to different objects.
Equal and same hash. Both hold 1000, so equals is true and the hash codes are both 1000.
The set checks both. It uses hashCode to find the bucket and equals to confirm a match.
One entry. The second add finds an equal element and changes nothing, so the size is 1.
Frequently Asked Questions
It is the number itself. Integer.valueOf(42).hashCode() returns 42, and -7 returns -7.
The instance method works on an Integer object. The static method, added in Java 8, takes a primitive int and returns the same value without creating an object.
No. For Integer the hash code is the value, so different values have different hash codes. Other classes can have collisions.
Hash-based collections use hashCode to find a bucket and equals to confirm. If equal objects had different hash codes, lookups would fail.
Use Objects.hash(a, b, c), or the recipe result = 31 * result + Integer.hashCode(field) for each field.