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.


What is the Set Interface in Java?

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.

Key Characteristics of Set:

  • Unique Elements: A Set does not allow duplicates.
  • Unordered: The elements are not stored in any specific order unless an implementation like LinkedHashSet or TreeSet is used.
  • No Indexing: Unlike lists, you cannot access elements in a set by index.

Common Implementations of the Set Interface

There are several common implementations of the Set interface in Java, each with its unique characteristics:

1. HashSet

  • Description: The most commonly used Set implementation, backed by a hash table. It does not maintain any order of elements.
  • Thread-Safety: Not thread-safe.
  • Null Elements: Allows at most one 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]

2. LinkedHashSet

  • Description: This implementation maintains the insertion order of the elements. It is backed by a hash table and a linked list.
  • Thread-Safety: Not thread-safe.
  • Null Elements: Allows at most one 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]

3. TreeSet

  • Description: A Set implementation that maintains elements in a sorted order. It is backed by a Red-Black tree.
  • Thread-Safety: Not thread-safe.
  • Null Elements: Does not allow 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]

4. EnumSet

  • Description: A specialized Set implementation for enum types. It is extremely efficient and supports all basic Set operations.
  • Thread-Safety: Not thread-safe.
  • Null Elements: Does not allow null elements.
enum Fruit { APPLE, BANANA, MANGO }
Set<Fruit> enumSet = EnumSet.of(Fruit.APPLE, Fruit.BANANA);
System.out.println(enumSet); // Output: [APPLE, BANANA]

Core Methods of the Set Interface

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:

1. add(E e)

This method adds an element to the set. If the element already exists, it will not be added.

boolean add(E e);

2. remove(Object o)

This method removes the specified element from the set.

boolean remove(Object o);

3. contains(Object o)

This method checks if a specified element is present in the set.

boolean contains(Object o);

4. size()

This method returns the number of elements in the set.

int size();

5. isEmpty()

This method checks if the set is empty.

boolean isEmpty();

6. clear()

This method removes all elements from the set.

void clear();

7. iterator()

This method returns an iterator over the elements in the set.

Iterator<E> iterator();

8. forEach(Consumer<? super E> action)

This method performs the specified action on each element of the set.

void forEach(Consumer<? super E> action);

Example: Using Set in Java

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: []

When to Use Set in Java

You should use the Set interface in Java when:

  1. Uniqueness of Elements is Important: If you want to store only unique elements and automatically eliminate duplicates.
  2. Fast Lookup: When you need fast lookup, insertion, and deletion operations, especially in implementations like HashSet.
  3. Sorted Collection: If you need elements to be stored in a sorted order, consider using TreeSet.
  4. No Duplicates Allowed: When the order of elements doesn't matter, but you need to guarantee that no duplicates are allowed.

Set vs List

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