Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

The Set interface

Reference for java.util.Set: collections without duplicates, add() return values, HashSet versus LinkedHashSet versus TreeSet, Set.of() and equality rules.

java.util.Set<E> is a collection that cannot contain duplicate elements. Adding an element that is already present does nothing and add() returns false. Membership tests are what sets are built for, and the common implementations answer contains() in constant time (HashSet) or logarithmic time (TreeSet).

Set adds no new methods to Collection, but it tightens their contracts: no duplicates, and equals() compares sets by content regardless of order. Whether iteration order is defined depends on the implementation: HashSet makes no promise, LinkedHashSet keeps insertion order and TreeSet keeps sorted order.

Java
Set<String> seen = new HashSet<>();
if (!seen.add(word)) { /* duplicate */ }

Try it

Java Duplicates and add() results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Three implementations and Set.of()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public interface Set<E> extends Collection<E>
Member Description
boolean add(E e) Adds e if absent; returns true if the set changed.
boolean remove(Object o) Removes o if present.
boolean contains(Object o) Membership test.
addAll, retainAll, removeAll, containsAll Union, intersection, difference and subset tests.
static <E> Set<E> of(E... elements) Immutable set (Java 9); duplicates throw IllegalArgumentException.
static <E> Set<E> copyOf(Collection<? extends E> c) Immutable copy (Java 10); duplicates are ignored.

Throws: UnsupportedOperationException on immutable sets; NullPointerException for null in sets that forbid it (Set.of(), TreeSet with natural order); ClassCastException for elements that cannot be compared.

Usage notes

  • Equality: HashSet uses equals()/hashCode(); TreeSet uses compareTo()/the comparator. A comparator that treats two different objects as equal makes TreeSet drop one of them.
  • Mutable elements: changing an element's fields after insertion can make it unfindable in a HashSet. Use immutable elements.
  • Order: never depend on HashSet or Set.of() iteration order; it can differ between runs and Java versions.
  • null: HashSet and LinkedHashSet accept one null; TreeSet with natural ordering and Set.of() reject it.
  • Set algebra with bulk methods is shown on Set operations.
  • No indexed access: convert to a list when you need positions.
  • Thread safety: none by default; see ConcurrentHashMap.newKeySet() and CopyOnWriteArraySet.