Home Java HashSet Methods addAll() Java HashSet addAll() Method 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.
Set<String> skills = new HashSet<>();
skills.addAll(Arrays.asList("java", "sql", "java"));
System.out.println(skills.size()); // 2 Call What 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 // Inherited from java.util.AbstractCollection<E>
public boolean addAll(Collection<? extends E> c)
// Declared in Set<E> and Collection<E> Parameters Parameter Description c (Collection<? extends E>)The collection whose elements are added. It must not be null, and it is only read.
Return Value Return Value Description trueAt least one element was new, so the set changed. falseEvery element was already present, or c was empty.
Important
Passing null throws NullPointerException. The new elements are read in the iteration order of c, but the set still prints in its own hash order. On an unmodifiable set, such as one from Collections.unmodifiableSet , the call throws UnsupportedOperationException.
Compare with Similar Methods Set operations built from the bulk methods:
Java Examples 1. Union of two sets 2. Remove list duplicates 3. Return value and null Example 1 — Combine Two Skill Lists into One Merge two sets of skills with addAll() and print the union in sorted order.
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));
}
} changed: true
union: [docker, git, java, sql]
changed again: false How It Works Three new elements. "git" and "docker" were missing from backend, so the first call returns true; "sql" was already there and is skipped.Sorted for display. Copying into a TreeSet prints the five distinct skills alphabetically, whatever the hash order inside backend.Nothing left to add. The second addAll(tools) finds every skill already present and returns false.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.
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 A list goes in as is. addAll(emails) accepts a List because the parameter is any Collection; the list itself is not modified.Repeats collapse. "[email protected] " and "[email protected] " each appear twice in the list, but the set keeps one copy of each, leaving three addresses.Back to a list. A set has no positions, so the values are copied into an ArrayList to sort them with Collections.sort.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.
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");
}
}
} empty list: false
known values: false
one new value: true
size: 4
null -> NullPointerException How It Works Empty argument. There is nothing to add, so addAll(none) returns false and the set stays at three elements.Known values. 3 and 2 are already stored, so the second call is also false.One new value is enough. 9 is new, so the third call returns true even though 2 is skipped; the size becomes 4.No null collections. addAll(null) cannot be read, so it throws NullPointerException, which the catch block reports.Frequently Asked Questions What does HashSet addAll() do? It adds every element of another collection to the set and skips elements that are already present. It returns true if the set changed.
How do I get the union of two sets in Java? Copy the first set with new HashSet<>(a), then call copy.addAll(b). The copy then holds every element from both sets once.
How do I remove duplicates from a list with a HashSet? 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.
Does addAll() throw an exception for a null collection? Yes. addAll(null) throws NullPointerException. A collection that contains null elements is fine because HashSet accepts one null.
Does addAll() change the collection I pass in? No. The argument is only read; only the set you call addAll() on can change.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion