Java HashSet hashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

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
CallWhat 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>> groupsA 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

ParameterDescription
NonehashCode() takes no parameters.

Return Value

Return ValueDescription
intThe sum of the hash codes of the elements. An empty set returns 0.

Compare with Similar Methods

Related methods:

MethodHow it differs
equals(o)Equal sets must have equal hash codes; this is the full comparison.
contains(e)Uses the element's hash code to find a bucket.
List.hashCode()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());
    }
}

How It Works

  1. The number is its own hash. An Integer hashes to its value, so the set hashes to 10 + 20 + 30 = 60.
  2. Order does not matter. Addition gives the same total whichever order the elements are visited in, so b also returns 60.
  3. Nothing to add up. An empty set has no elements, so its hash code is 0.
  4. 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));
    }
}

How It Works

  1. Character codes. The one-letter strings "a" and "b" hash to their character codes 97 and 98, so the set totals 195.
  2. Same as the manual sum. Adding the two String hash codes yourself gives the same 195.
  3. One element, one hash. A set holding only "Java" returns exactly the hash code of that string, 2301506.
  4. 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());
    }
}

How It Works

  1. Sets as elements. first and second hold 1 and 2 in different orders, so they are equal and share the hash code 3.
  2. The duplicate is rejected. groups.add(second) returns false because an equal set is already stored, which leaves one group.
  3. 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.
  4. 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.
0, because there are no elements to add up.

Next: isEmpty()

Check whether the set has no elements.

isEmpty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful