Java Set Interface
In Java, the Set
interface is a part of the java.util
package and is a collection that doesn't allow duplicate elements. It models the mathematical set abstraction and is one of the core collection interfaces in Java, often used when the uniqueness of elements is important. A Set
is a collection that does not guarantee any specific order of its elements, although some implementations like LinkedHashSet
and TreeSet
offer predictable ordering.
The Set
interface represents a collection of elements in which no two elements are the same. In other words, it is a mathematical set that enforces uniqueness. Since it extends the Collection
interface, a Set
is a subtype of Collection
, and it inherits all the standard collection methods like add()
, remove()
, clear()
, etc.
Set
does not allow duplicates.LinkedHashSet
or TreeSet
is used.There are several common implementations of the Set
interface in Java, each with its unique characteristics:
Set
implementation, backed by a hash table. It does not maintain any order of elements.null
element.
Set<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Apple"); // Duplicate, will not be added
System.out.println(hashSet); // Output: [Apple, Banana]
null
element.
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Apple");
linkedHashSet.add("Banana");
linkedHashSet.add("Apple"); // Duplicate, will not be added
System.out.println(linkedHashSet); // Output: [Apple, Banana]
Set
implementation that maintains elements in a sorted order. It is backed by a Red-Black tree.null
elements (because it uses comparison to maintain order).
Set<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Mango");
treeSet.add("Apple"); // Duplicate, will not be added
System.out.println(treeSet); // Output: [Apple, Banana, Mango]
Set
implementation for enum types. It is extremely efficient and supports all basic Set
operations.null
elements.
enum Fruit { APPLE, BANANA, MANGO }
Set<Fruit> enumSet = EnumSet.of(Fruit.APPLE, Fruit.BANANA);
System.out.println(enumSet); // Output: [APPLE, BANANA]
The Set
interface extends Collection
, so it inherits many of the methods from the Collection
interface. Below are some key methods of the Set
interface:
This method adds an element to the set. If the element already exists, it will not be added.
boolean add(E e);
This method removes the specified element from the set.
boolean remove(Object o);
This method checks if a specified element is present in the set.
boolean contains(Object o);
This method returns the number of elements in the set.
int size();
This method checks if the set is empty.
boolean isEmpty();
This method removes all elements from the set.
void clear();
This method returns an iterator over the elements in the set.
Iterator<E> iterator();
This method performs the specified action on each element of the set.
void forEach(Consumer<? super E> action);
Here’s an example demonstrating the use of Set
and its various methods:
import java.util.*;
public class SetExample {
public static void main(String[] args) {
// Create a HashSet (no order)
Set<String> fruits = new HashSet<>();
// Add elements to the set
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // Duplicate, will not be added
// Check if an element exists in the set
System.out.println("Contains 'Banana': " + fruits.contains("Banana")); // true
// Remove an element
fruits.remove("Orange");
// Print all elements
System.out.println("Fruits in the set: " + fruits); // Output: [Apple, Banana]
// Iterate through the set
for (String fruit : fruits) {
System.out.println(fruit); // Output: Apple, Banana (order may vary)
}
// Get the size of the set
System.out.println("Set size: " + fruits.size()); // Output: 2
// Check if the set is empty
System.out.println("Is set empty? " + fruits.isEmpty()); // false
// Clear the set
fruits.clear();
System.out.println("Set after clear: " + fruits); // Output: []
}
}
Output:
Contains 'Banana': true
Fruits in the set: [Apple, Banana]
Apple
Banana
Set size: 2
Is set empty? false
Set after clear: []
You should use the Set
interface in Java when:
HashSet
.TreeSet
.Here's a quick comparison between Set
and List
:
Feature | Set |
List |
---|---|---|
Order | No order or insertion order (depends on implementation). | Maintains insertion order. |
Duplicates | Does not allow duplicates. | Allows duplicates. |
Indexing | No indexing, cannot access by index. | Elements can be accessed by index. |
Common Implementations | HashSet , TreeSet , LinkedHashSet |
ArrayList , LinkedList |