JavaSet Interface

Set Interface

A Set is a collection that contains no duplicate elements. It models the mathematical idea of a set: an element is either in the set or it isn't — there's no concept of it appearing twice. Set extends Collection but adds no new methods of its own; what makes it distinct is the contract its implementations must honor.

Duplicates are silently ignored

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

Set<String> languages = new HashSet<>();
languages.add("Java");
languages.add("Python");
languages.add("Java"); // ignored — already present

System.out.println(languages);
System.out.println(languages.size());
[Java, Python]
2
No Guaranteed Order

The Set interface itself makes no promises about iteration order. Whether the order you get back is unpredictable, sorted, or based on insertion order depends entirely on which implementation you choose:

Implementation

Ordering

HashSet

No guaranteed order at all

LinkedHashSet

Insertion order

TreeSet

Sorted order (natural or custom Comparator)

These three implementations are covered in depth on the HashSet & TreeSet page — this page focuses on what all of them share as members of the Set family.
Set Algebra: Union, Intersection, Difference
Because a Set models mathematical set theory, Java lets you perform the classic set operations directly using bulk methods inherited from Collection: addAll() for union, retainAll() for intersection, and removeAll() for difference.

Union, intersection, and difference

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

Set<Integer> a = new HashSet<>(Set.of(1, 2, 3, 4));
Set<Integer> b = new HashSet<>(Set.of(3, 4, 5, 6));

// Union: everything in either set
Set<Integer> union = new HashSet<>(a);
union.addAll(b);
System.out.println("Union: " + union);

// Intersection: only what's in both sets
Set<Integer> intersection = new HashSet<>(a);
intersection.retainAll(b);
System.out.println("Intersection: " + intersection);

// Difference: what's in 'a' but not in 'b'
Set<Integer> difference = new HashSet<>(a);
difference.removeAll(b);
System.out.println("Difference: " + difference);
Union: [1, 2, 3, 4, 5, 6]
Intersection: [3, 4]
Difference: [1, 2]
Note
Each of these operations mutates the set they're called on. That's why the example first copies a into a new HashSet — so the original sets a and b are left untouched for the next calculation.
Checking Membership and Uniqueness
A Set's contains() method is where its real value shows up. A well-implemented Set (HashSet in particular) can answer "is this element in the collection?" in constant time on average, versus the linear scan a List would need.

Deduplicating a list with a Set

Java
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

List<String> withDuplicates =
    Arrays.asList("red", "green", "red", "blue", "green");

Set<String> unique = new LinkedHashSet<>(withDuplicates);
System.out.println(unique);
[red, green, blue]
When to Reach for a Set Instead of a List
  • You need to guarantee an element only ever appears once

  • You care about fast membership testing (contains())

  • You don't care about maintaining a particular access order

  • You want to perform set algebra: union, intersection, difference

Tip
Reach for a Set whenever uniqueness matters and order doesn't — or when you need much faster contains() checks than a List can offer. If you find yourself calling list.contains(x) in a loop over a large List, that's usually a sign the data belongs in a Set instead.