← Back to Core Java Course | Chapter 11: Collections | Lesson 8 of 17

Java Set Interface

The Set interface represents a collection that contains no duplicate elements, used whenever the uniqueness of the stored values matters more than the order they were added in.

What is the Set Interface?

The Set interface represents a collection that contains no duplicate elements, and is used whenever the uniqueness of the stored values matters more than the order they were added in.

Example: What is the Set Interface?

java
import java.util.Set;
import java.util.HashSet;
public class Main {
	public static void main(String[] args) {
		Set<String> names = new HashSet<>();
		names.add("Alice");
		names.add("Alice"); // duplicate ignored
		System.out.println(names.size());
	}
}

Set Implementations

HashSet, LinkedHashSet, and TreeSet are the three standard implementations of Set: HashSet offers the fastest operations with no ordering guarantee, LinkedHashSet preserves insertion order, and TreeSet keeps elements sorted.

Example: Set Implementations

java
import java.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		Set<String> hash = new HashSet<>();
		Set<String> linked = new LinkedHashSet<>(); // insertion order
		Set<String> tree = new TreeSet<>(); // sorted
		hash.add("a"); linked.add("a"); tree.add("a");
		System.out.println(hash + " " + linked + " " + tree);
	}
}

No Duplicates Allowed

Every implementation of Set silently ignores an attempt to add a value that's already present -- the add method returns false instead of throwing an error, and the set's size never grows from a duplicate insertion.

Example: No Duplicates Allowed

java
import java.util.Set;
import java.util.HashSet;
public class Main {
	public static void main(String[] args) {
		Set<String> names = new HashSet<>();
		System.out.println(names.add("Alice")); // true
		System.out.println(names.add("Alice")); // false: already present
	}
}

Common Set Methods

The Set interface defines methods like add, remove, contains, and size that work the same way across every implementation, letting code switch between HashSet, TreeSet, or LinkedHashSet without changing how it interacts with the set.

Example: Common Set Methods

java
import java.util.Set;
import java.util.HashSet;
public class Main {
	public static void main(String[] args) {
		Set<String> names = new HashSet<>();
		names.add("Alice");
		System.out.println(names.contains("Alice"));
		System.out.println(names.size());
		names.remove("Alice");
		System.out.println(names.contains("Alice"));
	}
}

Choosing a Set Implementation

Choosing between Set implementations comes down to what guarantee you need: HashSet for the fastest general-purpose set, LinkedHashSet when insertion order must be remembered, and TreeSet when the elements must always stay sorted.

Example: Choosing a Set Implementation

java
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		Set<Integer> fast = new HashSet<>(); // fastest, no order
		Set<Integer> sorted = new TreeSet<>(); // sorted order
		fast.add(3);
		sorted.add(3);
		System.out.println(fast + " " + sorted);
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.