Set Interface in Java

The Set interface is a collection that does not allow duplicate elements. It models a mathematical set and is mainly used when uniqueness is required.


Characteristics


Interface Hierarchy


Collection<E>
|
Set<E>
|
+-- HashSet<E>
+-- LinkedHashSet<E>
+-- TreeSet<E>
+-- EnumSet<E>

Common Implementations

HashSet

Set<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Red"); // Duplicate ignored
System.out.println(colors); // [Red, Green]

LinkedHashSet

Set<String> names = new LinkedHashSet<>();
names.add("Alice");
names.add("Bob");
System.out.println(names); // [Alice, Bob]

TreeSet

Set<Integer> numbers = new TreeSet<>();
numbers.add(10);
numbers.add(5);
numbers.add(20);
System.out.println(numbers); // [5, 10, 20]

EnumSet

enum Role { ADMIN, USER, GUEST }
Set<Role> roles = EnumSet.of(Role.ADMIN, Role.USER);

Core Methods

Method Description
add(E e) Adds an element if not already present
remove(Object o) Removes the specified element
contains(Object o) Checks if element exists
size() Returns the number of elements
clear() Removes all elements
isEmpty() Checks if the set is empty
iterator() Iterates through the set

Convenient Set Methods

Set.of(…)

Creates an immutable set.

Set<String> tags = Set.of("A", "B", "C");

Collections.emptySet()

Returns a shared immutable empty set.

Set<String> empty = Collections.emptySet();

Collections.singleton(…)

Creates an immutable set with one element.

Set<String> onlyOne = Collections.singleton("ROOT");

Set.copyOf(…)

Creates an immutable copy of another set.

Set<String> copy = Set.copyOf(tags);

Performance Comparison

Implementation Ordering Null Allowed Thread-safe Lookup Time
HashSet No order Yes No O(1)
LinkedHashSet Insertion order Yes No O(1)
TreeSet Sorted No No O(log n)
EnumSet Enum-defined No No O(1)

When to Use


Example

Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
uniqueNames.add("Bob");
uniqueNames.add("Alice"); // ignored

System.out.println(uniqueNames.contains("Bob")); // true
System.out.println(uniqueNames.size());          // 2

Summary

The Set interface is essential when you need to enforce uniqueness in your data. Each implementation provides a different trade-off in terms of performance, ordering, and memory usage. Choosing the right Set can lead to cleaner and more efficient code.

By understanding and applying the right Set type, you eliminate redundancy and write logic that naturally enforces data integrity.


Classes
Quiz
Videos
References
Books