What Is Set Data Structure in Java?
In Java, a Set is a data structure that stores a collection of unique elements. Unlike a List, which allows duplicate elements, a Set ensures that each element is unique.
If you try to add an element that already exists in the Set, it will simply be ignored. Sets are commonly used when you need to maintain a collection of distinct values without any specific order.
Creating a Set
To create a Set in Java, you need to import the java.util.Set interface and choose an implementation class such as HashSet, TreeSet, or LinkedHashSet. Here’s an example of creating a HashSet:
import java.HashSet;
import java.Set;
Set<String> set = new HashSet<>();
In this example, we import the necessary classes and then declare a variable ‘set’ of type ‘Set
Adding Elements to a Set
To add elements to a set, you can use the add() method provided by the Set interface. Here’s how you can add elements to our set:
set.add("Apple");
set.add("Banana");
set.add("Orange");
set.add("Apple"); // Ignored
In this example, we add four elements to our set: “Apple”, “Banana”, “Orange”, and “Apple” again. Notice that the second occurrence of “Apple” is ignored because Sets do not allow duplicates.
Removing Elements from a Set
To remove elements from a set, you can use the remove() method. Here’s an example:
set.remove("Banana");
After executing this code, the element “Banana” will be removed from the set if it exists.
Checking if an Element Exists in a Set
To check if an element exists in a set, you can use the contains() method. Here’s an example:
boolean containsApple = set.contains("Apple");
In this example, we check if the element “Apple” exists in our set and store the result in the ‘containsApple’ variable. The value will be ‘true’ if “Apple” is present and ‘false’ otherwise.
Iterating over a Set
To iterate over the elements of a set, you can use either an enhanced for loop or an iterator. Here’s an example using an enhanced for loop:
for (String element : set) {
System.out.println(element);
}
This code will print each element of the set on a new line.
Set Operations
Sets also support various operations such as union, intersection, and difference. However, these operations are not directly available in the Set interface.
To perform these operations, you can use methods provided by other classes such as ‘addAll()’, ‘retainAll()’, and ‘removeAll()’. These methods modify the original set or return a new set based on the operation performed.
Example: Union of Two Sets
Set<String> set1 = new HashSet<>();
set1.add("Apple");
set1.add("Banana");
Set<String> set2 = new HashSet<>();
set2.add("Banana");
set2.add("Orange");
Set<String> union = new HashSet<>(set1);
union.addAll(set2);
System.println(union);
In this example, we create two sets ‘set1’ and ‘set2’ with some elements. Then, we create a new set ‘union’ by adding all elements from both sets using the ‘addAll()’ method.
The output will be: [Apple, Banana, Orange].
Conclusion
In this tutorial, you learned about the Set data structure in Java. Sets are useful when you need to store a collection of unique elements without any specific order. You can create a Set using various implementations such as HashSet, TreeSet, or LinkedHashSet.
Sets provide methods to add, remove, check existence of elements, and perform set operations. Make sure to choose the appropriate implementation based on your requirements.