CKTIS / Java
Set.add — Add an element without duplicates
`Set.add` returns `true` when it inserts an element and `false` when an equivalent element is already present. `HashSet` and `TreeSet` identify duplicates differently.
Version: Java SE 21
Syntax
boolean add(E e);Set.add is an instance method of java.util.Set<E>, where E is the element type. If addition is supported and the argument is valid for the set, set.add(element) returns true when it inserts a new element. If an equivalent element is already present, it returns false: the set does not grow, and the existing element is not replaced. You can use the return value to tell whether insertion happened without calling contains first.
The Set contract defines duplicates using Objects.equals. HashSet uses hashCode and equals: equal objects must have the same hash code, but matching hash codes alone do not make objects equal. With the generated methods, two records of the same class with equal corresponding component values are equal and have the same hash code. HashSet does not guarantee an iteration order.
TreeSet iterates in natural order or in the order defined by a supplied Comparator. It treats elements as duplicates when compareTo or compare, respectively, returns 0. The ordering should be consistent with equals: comparison should return 0 exactly when the elements are equal according to equals. Otherwise, the TreeSet does not meet the general Set contract.
Addition is optional: an unmodifiable set from Set.of throws UnsupportedOperationException even for a duplicate. HashSet allows one null element, while a naturally ordered TreeSet rejects null with NullPointerException. Depending on the implementation, an incompatible argument type can cause ClassCastException, and a disallowed element property can cause IllegalArgumentException.
Examples
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Main {
record Ticket(String name) {}
public static void main(String[] args) {
Set<Ticket> tickets = new HashSet<>();
System.out.println(tickets.add(new Ticket("Ada")));
System.out.println(tickets.add(new Ticket("Lin")));
System.out.println(tickets.add(new Ticket("Sam")));
System.out.println(tickets.add(new Ticket("Ada")));
System.out.println("Tickets: " + tickets.size());
Set<Integer> numbers = new TreeSet<>();
numbers.add(3);
numbers.add(1);
numbers.add(2);
System.out.println("Duplicate 1: " + numbers.add(1));
System.out.println("TreeSet:");
for (int number : numbers) {
System.out.println(number);
}
}
}Result
true true true false Tickets: 3 Duplicate 1: false TreeSet: 1 2 3
The fourth `Ticket` is a new object but equals the first, so `add` returns `false` and the size stays at `3`. Adding `1` again to the `TreeSet` also returns `false`, and iteration prints `1`, `2`, `3` in natural order.
Related lessons
- Java · Lesson 5 · Advanced · Version: 2
- Java · Lesson 8 · Advanced · Version: 2
- Java · Lesson 27 · Basic · Version: 2
- Java · Lesson 42 · Basic · Version: 2
- Java · Lesson 4 · Intermediate · Version: 2
- Java · Lesson 5 · Intermediate · Version: 2