Java Collections: Sets

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:February 25, 2024

In Java, a Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction and provides operations for basic set operations like union, intersection, difference, etc. In this tutorial, we’ll explore the various implementations of the Set interface in the Java Collections framework and how to use them.

Table of Contents

  1. Introduction to Sets
  2. HashSet
  3. LinkedHashSet
  4. TreeSet
  5. Common Operations
  1. Set Operations
  1. Conclusion

1. Introduction to Sets

A Set in Java is a collection that does not allow duplicate elements. It extends the Collection interface and adds the constraint that no two elements in the Set can be equal according to the equals() method. The Set interface does not guarantee the order of elements, and it does not provide access by index like a List.

import java.util.Set;

Common implementations of the Set interface in Java Collections are:

  • HashSet
  • LinkedHashSet
  • TreeSet

2. HashSet

HashSet is the most commonly used implementation of the Set interface. It stores elements using a hash table, which provides constant-time performance for basic operations like add, remove, contains, and size. However, it does not maintain the insertion order of elements.

Creating a HashSet

import java.util.HashSet;
import java.util.Set;

Set<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Orange");

System.out.println(hashSet); // [Apple, Orange, Banana]

3. LinkedHashSet

LinkedHashSet is an implementation of the Set interface that maintains the insertion order of elements. It is implemented as a hash table with a linked list running through it, so it provides a predictable iteration order.

Creating a LinkedHashSet

import java.util.LinkedHashSet;
import java.util.Set;

Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Apple");
linkedHashSet.add("Banana");
linkedHashSet.add("Orange");

System.out.println(linkedHashSet); // [Apple, Banana, Orange]

4. TreeSet

TreeSet is an implementation of the SortedSet interface, which maintains the elements in sorted order. It uses a Red-Black tree structure internally to achieve this. Elements in a TreeSet must either implement the Comparable interface or be provided with a Comparator.

Creating a TreeSet

import java.util.TreeSet;
import java.util.Set;

Set<String> treeSet = new TreeSet<>();
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Orange");

System.out.println(treeSet); // [Apple, Banana, Orange]

5. Common Operations

Adding Elements

Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");

Removing Elements

set.remove("Banana");

Checking Existence

boolean containsOrange = set.contains("Orange");

Iteration

for (String element : set) {
    System.out.println(element);
}

6. Set Operations

Union

Set<String> set1 = new HashSet<>();
set1.add("Apple");
set1.add("Banana");

Set<String> set2 = new HashSet<>();
set2.add("Orange");
set2.add("Banana");

Set<String> union = new HashSet<>(set1);
union.addAll(set2);

System.out.println(union); // [Apple, Banana, Orange]

Intersection

Set<String> intersection = new HashSet<>(set1);
intersection.retainAll(set2);

System.out.println(intersection); // [Banana]

Difference

Set<String> difference = new HashSet<>(set1);
difference.removeAll(set2);

System.out.println(difference); // [Apple]

7. Conclusion

Sets in Java Collections are useful when you need a collection that does not allow duplicates. The three main implementations, HashSet, LinkedHashSet, and TreeSet, provide different behaviors based on your requirements. Remember that HashSet offers constant-time performance, LinkedHashSet maintains insertion order, and TreeSet maintains elements in sorted order.

Understanding these implementations and their common operations allows you to effectively work with sets in Java for various tasks.

Additional Resources:

Leave a Reply