Java Set Interface
In this page:
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?
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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"));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting