Java HashSet addAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In Java, addAll(c) adds every element of the collection c to a HashSet. Elements that are already in the set are skipped, so the result is the union of the two collections when c is a set.

The argument can be any collection: another set, a List, or the result of Arrays.asList. That makes addAll() the standard way to remove duplicates from a list.

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 collection to read from.

Java
Set<String> skills = new HashSet<>();
skills.addAll(Arrays.asList("java", "sql", "java"));
System.out.println(skills.size());   // 2
CallWhat it does
a.addAll(b)Adds every element of b to a; b is not changed.
a.addAll(Arrays.asList("x", "y"))Adds several literals at once.
if (a.addAll(b)) { ... }Runs the block only when at least one element was new.
new HashSet<>(list)The constructor does the same job when you create the set.

Only the receiving set changes. If you need the union as a new set, copy first with new HashSet<>(a) and call addAll() on the copy.

Library declarations
Java
// Inherited from java.util.AbstractCollection<E>
public boolean addAll(Collection<? extends E> c)

// Declared in Set<E> and Collection<E>

Parameters

ParameterDescription
c (Collection<? extends E>)The collection whose elements are added. It must not be null, and it is only read.

Return Value

Return ValueDescription
trueAt least one element was new, so the set changed.
falseEvery element was already present, or c was empty.

Compare with Similar Methods

Set operations built from the bulk methods:

MethodHow it differs
add(e)Adds one element at a time.
retainAll(c)Keeps only the shared elements: the intersection.
removeAll(c)Removes the shared elements: the difference.
Collections.addAll(c, ...)Adds individual elements or an array instead of a collection.

Java Examples

Example 1 — Combine Two Skill Lists into One

Merge two sets of skills with addAll() and print the union in sorted order.

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

public class HashSetAddAllUnion {
    public static void main(String[] args) {
        Set<String> backend = new HashSet<>();
        backend.add("java");
        backend.add("sql");

        Set<String> tools = new HashSet<>();
        tools.add("sql");
        tools.add("git");
        tools.add("docker");

        boolean changed = backend.addAll(tools);
        System.out.println("changed: " + changed);
        System.out.println("union: " + new TreeSet<>(backend));
        System.out.println("changed again: " + backend.addAll(tools));
    }
}

How It Works

  1. Three new elements. "git" and "docker" were missing from backend, so the first call returns true; "sql" was already there and is skipped.
  2. Sorted for display. Copying into a TreeSet prints the five distinct skills alphabetically, whatever the hash order inside backend.
  3. Nothing left to add. The second addAll(tools) finds every skill already present and returns false.
  4. Argument untouched. tools still holds its own three elements after the merge.

Example 2 — Remove Duplicate E-mails from a List

Pour a list with repeats into a set, then read the unique values back into a sorted list.

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

public class HashSetAddAllDedupe {
    public static void main(String[] args) {
        List<String> emails = Arrays.asList(
                "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]");

        Set<String> unique = new HashSet<>();
        unique.addAll(emails);

        List<String> sorted = new ArrayList<>(unique);
        Collections.sort(sorted);

        System.out.println("list size: " + emails.size());
        System.out.println("unique size: " + unique.size());
        System.out.println("sorted: " + sorted);
    }
}

How It Works

  1. A list goes in as is. addAll(emails) accepts a List because the parameter is any Collection; the list itself is not modified.
  2. Repeats collapse. "[email protected]" and "[email protected]" each appear twice in the list, but the set keeps one copy of each, leaving three addresses.
  3. Back to a list. A set has no positions, so the values are copied into an ArrayList to sort them with Collections.sort.
  4. Sizes tell the story. Five entries in, three out: list size stays 5 while unique size is 3.

Example 3 — When addAll() Returns false, and the null Argument

Try an empty list, a list of known elements, a list with a new element, and a null argument.

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

public class HashSetAddAllResult {
    public static void main(String[] args) {
        Set<Integer> numbers = new HashSet<>(Arrays.asList(1, 2, 3));

        List<Integer> none = new ArrayList<>();
        System.out.println("empty list: " + numbers.addAll(none));
        System.out.println("known values: " + numbers.addAll(Arrays.asList(3, 2)));
        System.out.println("one new value: " + numbers.addAll(Arrays.asList(2, 9)));
        System.out.println("size: " + numbers.size());

        try {
            numbers.addAll(null);
        } catch (NullPointerException e) {
            System.out.println("null -> NullPointerException");
        }
    }
}

How It Works

  1. Empty argument. There is nothing to add, so addAll(none) returns false and the set stays at three elements.
  2. Known values. 3 and 2 are already stored, so the second call is also false.
  3. One new value is enough. 9 is new, so the third call returns true even though 2 is skipped; the size becomes 4.
  4. No null collections. addAll(null) cannot be read, so it throws NullPointerException, which the catch block reports.

Frequently Asked Questions

It adds every element of another collection to the set and skips elements that are already present. It returns true if the set changed.
Copy the first set with new HashSet<>(a), then call copy.addAll(b). The copy then holds every element from both sets once.
Call set.addAll(list), or create new HashSet<>(list). The set keeps one copy of each value; copy it back to a list if you need positions.
Yes. addAll(null) throws NullPointerException. A collection that contains null elements is fine because HashSet accepts one null.
No. The argument is only read; only the set you call addAll() on can change.

Next: clear()

Remove every element from the set.

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

8 people found this page helpful