Java HashSet equals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In Java, equals(o) tells you whether a HashSet is equal to another object. It returns true when o is also a Set, has the same size, and contains every element of this set.

The order of the elements does not matter, and the other set can be any Set implementation, such as TreeSet or LinkedHashSet. A List is never equal to a set, even with the same elements.

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

One argument: the object to compare with.

Java
Set<String> a = new HashSet<>(Arrays.asList("x", "y"));
Set<String> b = new HashSet<>(Arrays.asList("y", "x"));
boolean same = a.equals(b);   // true
CallWhat it does
a.equals(b)true if both sets hold the same elements.
a == btrue only if both variables point to the very same object.
Objects.equals(a, b)Null-safe version of equals.
a.equals(list)Always false: a set never equals a list.

The rule comes from the Set interface, so a HashSet equals a TreeSet with the same elements. Elements are compared with their own equals() method.

Library declarations
Java
// Inherited from java.util.AbstractSet<E>
public boolean equals(Object o)

// Contract defined by Set<E>

Parameters

ParameterDescription
o (Object)The object to compare with. It may be null, which gives false.

Return Value

Return ValueDescription
trueo is a Set of the same size that contains every element of this set.
falseo is null, is not a Set, or has different elements.

Compare with Similar Methods

Comparing sets and related checks:

MethodHow it differs
hashCode()Equal sets always return the same hash code.
containsAll(c)Checks one direction only (subset).
size()equals() compares sizes first.
== operatorCompares object identity, not contents.

Java Examples

Example 1 — Same Elements, Different Order

Build two sets in different orders, compare them, and then change one.

Java
import java.util.HashSet;
import java.util.Set;

public class HashSetEqualsDemo {
    public static void main(String[] args) {
        Set<String> a = new HashSet<>();
        a.add("x");
        a.add("y");
        a.add("z");

        Set<String> b = new HashSet<>();
        b.add("z");
        b.add("x");
        b.add("y");

        System.out.println("a equals b: " + a.equals(b));
        System.out.println("a == b: " + (a == b));

        b.add("w");
        System.out.println("after adding w to b: " + a.equals(b));

        a.add("w");
        System.out.println("after adding w to a: " + a.equals(b));
    }
}

How It Works

  1. Order is irrelevant. Both sets contain x, y, and z, so equals() is true although they were filled in different orders.
  2. Equal is not identical. a == b is false because the two variables point to two separate set objects.
  3. Sizes differ. Once b has a fourth element, the size check fails immediately and the answer is false.
  4. Equal again. Adding "w" to a makes the contents match again, so equals() returns true.

Example 2 — Equal Across Set Types, Not Across Lists or Number Types

Compare a HashSet with a TreeSet, an ArrayList, and a set of Long values.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class HashSetEqualsTypes {
    public static void main(String[] args) {
        Set<Integer> hash = new HashSet<>(Arrays.asList(1, 2, 3));
        Set<Integer> tree = new TreeSet<>(Arrays.asList(3, 2, 1));
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
        Set<Long> longs = new HashSet<>(Arrays.asList(1L, 2L, 3L));

        System.out.println("HashSet vs TreeSet: " + hash.equals(tree));
        System.out.println("HashSet vs ArrayList: " + hash.equals(list));
        System.out.println("Set<Integer> vs Set<Long>: " + hash.equals(longs));
    }
}

How It Works

  1. Any Set will do. The Set contract defines equality by contents, so a HashSet and a TreeSet holding 1, 2, and 3 are equal.
  2. A list is not a set. The ArrayList holds the same three numbers, but it is not a Set, so the answer is false.
  3. Elements must be equal too. 1L is a Long, and Integer.equals(Long) is false, so the two sets do not match.
  4. Compiles without complaint. equals() accepts any Object, so mixing types is not a compile error, just a false.

Example 3 — equals() and null: Objects.equals() to the Rescue

Compare a set with null, use the null-safe helper, and see what happens when the variable itself is null.

Java
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class HashSetEqualsNull {
    public static void main(String[] args) {
        Set<String> roster = new HashSet<>();
        roster.add("Ann");
        Set<String> missing = null;

        System.out.println("roster.equals(null): " + roster.equals(null));
        System.out.println("Objects.equals(roster, null): " + Objects.equals(roster, null));
        System.out.println("Objects.equals(missing, null): " + Objects.equals(missing, null));

        try {
            missing.equals(roster);
        } catch (NullPointerException e) {
            System.out.println("missing.equals(roster) -> NullPointerException");
        }
    }
}

How It Works

  1. null as the argument. A set is never equal to null, so roster.equals(null) is a quiet false.
  2. The helper agrees. Objects.equals(roster, null) is also false because exactly one side is null.
  3. Two nulls are equal. Objects.equals(missing, null) returns true when both sides are null.
  4. null as the receiver. Calling a method on the null variable missing throws NullPointerException; Objects.equals(a, b) avoids that by checking for null first.

Frequently Asked Questions

Call a.equals(b). It is true when both are sets of the same size containing the same elements, whatever their order.
No. Sets have no meaningful order, so equals() only checks which elements are present.
== checks whether two variables point to the same object. equals() checks whether two sets hold the same elements.
A HashSet equals any other Set with the same elements, including a TreeSet. It never equals a List, even with the same elements.
Set equality relies on the elements' own equals() and hashCode(). Without them, two sets of your own objects are equal only when they hold the very same object instances.

Next: hashCode()

Get one int that sums up the whole set.

hashCode() 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.

13 people found this page helpful