In Java, hashCode() returns an int that summarises the contents of a HashSet. The Set interface defines it as the sum of the hash codes of all the elements, where null counts as 0.
Because addition ignores order, two equal sets always return the same value. That is what allows a set to be used as an element of another set or as a key in a map.
An instance method of java.util.HashSet (class since Java 1.2): a set of unique elements kept in a hash table, with no guaranteed order, one null element allowed, and no thread safety.
Syntax
No arguments; returns an int.
Java
Set<Integer> s = new HashSet<>(Arrays.asList(10, 20, 30));
int h = s.hashCode(); // 60 = 10 + 20 + 30
Call
What it does
set.hashCode()
The sum of the element hash codes.
a.hashCode() == b.hashCode()
A quick test that is necessary for equal sets, but not sufficient.
Set<Set<Integer>> groups
A set used as an element of another set.
map.put(set, value)
A set used as a map key.
Integer.hashCode() is the number itself, and String.hashCode() follows a documented formula, so sets of these types give predictable totals that are the same on every JDK.
Library declarations
Java
// Inherited from java.util.AbstractSet<E>
public int hashCode()
// Contract defined by Set<E>
Parameters
Parameter
Description
None
hashCode() takes no parameters.
Return Value
Return Value
Description
int
The sum of the hash codes of the elements. An empty set returns 0.
Order-sensitive: it combines the elements with a multiplier, so reordering a list changes it.
Objects.hash(...)
The usual helper for the hashCode() of your own classes.
Java Examples
Example 1 — Hash Code of a Set of Numbers
Add up the hash codes of three numbers, reorder them, use an empty set, and add null.
Java
import java.util.HashSet;
import java.util.Set;
public class HashSetHashCodeDemo {
public static void main(String[] args) {
Set<Integer> a = new HashSet<>();
a.add(10);
a.add(20);
a.add(30);
Set<Integer> b = new HashSet<>();
b.add(30);
b.add(10);
b.add(20);
Set<Integer> empty = new HashSet<>();
System.out.println("a: " + a.hashCode());
System.out.println("b (same numbers, other order): " + b.hashCode());
System.out.println("empty set: " + empty.hashCode());
a.add(null);
System.out.println("a with null added: " + a.hashCode());
}
}
Output
a: 60
b (same numbers, other order): 60
empty set: 0
a with null added: 60
How It Works
The number is its own hash. An Integer hashes to its value, so the set hashes to 10 + 20 + 30 = 60.
Order does not matter. Addition gives the same total whichever order the elements are visited in, so b also returns 60.
Nothing to add up. An empty set has no elements, so its hash code is 0.
null adds zero. The set now has four elements, but null contributes 0, so the total stays 60.
Example 2 — Hash Codes of Sets of Strings, and a Collision
Compare a set hash with the sum of its String hash codes, then find two different sets with the same hash code.
Java
import java.util.HashSet;
import java.util.Set;
public class HashSetHashCodeStrings {
public static void main(String[] args) {
Set<String> ab = new HashSet<>();
ab.add("a");
ab.add("b");
System.out.println("set of a, b: " + ab.hashCode());
System.out.println("sum of the two strings: " + ("a".hashCode() + "b".hashCode()));
Set<String> word = new HashSet<>();
word.add("Java");
System.out.println("set of Java: " + word.hashCode());
System.out.println("\"Java\".hashCode(): " + "Java".hashCode());
Set<String> ad = new HashSet<>();
ad.add("a");
ad.add("d");
Set<String> bc = new HashSet<>();
bc.add("b");
bc.add("c");
System.out.println("hashes " + ad.hashCode() + " and " + bc.hashCode() + ", equal sets: " + ad.equals(bc));
}
}
Output
set of a, b: 195
sum of the two strings: 195
set of Java: 2301506
"Java".hashCode(): 2301506
hashes 197 and 197, equal sets: false
How It Works
Character codes. The one-letter strings "a" and "b" hash to their character codes 97 and 98, so the set totals 195.
Same as the manual sum. Adding the two String hash codes yourself gives the same 195.
One element, one hash. A set holding only "Java" returns exactly the hash code of that string, 2301506.
A collision. 97 + 100 and 98 + 99 are both 197, so {a, d} and {b, c} share a hash code even though equals() says they differ.
Example 3 — Sets Inside Sets, and the Mutation Trap
Store sets in a set of sets, then change one of them after it was added.
Java
import java.util.HashSet;
import java.util.Set;
public class HashSetHashCodeNested {
public static void main(String[] args) {
Set<Integer> first = new HashSet<>();
first.add(1);
first.add(2);
Set<Integer> second = new HashSet<>();
second.add(2);
second.add(1);
Set<Set<Integer>> groups = new HashSet<>();
System.out.println("add first: " + groups.add(first));
System.out.println("add second: " + groups.add(second));
System.out.println("groups: " + groups.size());
first.add(3);
System.out.println("first now hashes to " + first.hashCode());
System.out.println("groups.contains(first): " + groups.contains(first));
System.out.println("groups still has " + groups.size());
}
}
Output
add first: true
add second: false
groups: 1
first now hashes to 6
groups.contains(first): false
groups still has 1
How It Works
Sets as elements.first and second hold 1 and 2 in different orders, so they are equal and share the hash code 3.
The duplicate is rejected.groups.add(second) returns false because an equal set is already stored, which leaves one group.
Mutation changes the hash. After first.add(3) the set hashes to 1 + 2 + 3 = 6, but groups filed it under the old hash code 3.
Lost in place.contains(first) now looks in the bucket for 6 and finds nothing, although the set is still inside groups; the size stays 1.
Frequently Asked Questions
It is the sum of the hash codes of all the elements, with null counted as 0. The Set interface specifies this, so it is the same on every JDK.
Addition does not depend on order, and equal sets contain equal elements with equal hash codes, so both sets give the same total.
Yes. {1, 4} and {2, 3} both give 5. Equal hash codes are necessary for equal sets but do not prove equality; equals() decides.
Yes, but never change the set afterwards. Its hash code changes with its contents, and the map will no longer find the key.