Introduction
In Java 8, converting a stream to a set is useful when you want to collect unique elements. By using Collectors.toSet()
, you can easily convert a stream into a set, ensuring that any duplicate elements are removed automatically. The Stream API provides a flexible way to process and then collect the results into a set.
In this guide, we will learn how to convert a stream into a set in Java 8.
Solution Steps
- Create or Obtain a Stream: Generate or obtain a stream from a collection or another data source.
- Process the Stream (Optional): Perform any filtering, mapping, or other stream operations as needed.
- Use
collect(Collectors.toSet())
: Collect the elements of the stream into a set. - Display or Use the Set: Use or print the resulting set.
Java Program
Example 1: Convert Stream of Integers to a Set
import java.util.stream.Stream;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamToSetExample {
public static void main(String[] args) {
// Step 1: Create a stream of integers
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 3, 2);
// Step 2: Convert the stream to a set
Set<Integer> numberSet = numberStream.collect(Collectors.toSet());
// Step 3: Display the set
System.out.println(numberSet); // Output: [1, 2, 3, 4, 5]
}
}
Output
[1, 2, 3, 4, 5]
Explanation
Step 1: Create a Stream
We create a stream of integers, where some elements are duplicates:
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 3, 2);
Step 2: Convert the Stream to a Set
We use collect(Collectors.toSet())
to convert the stream into a set. The set automatically removes duplicate elements:
Set<Integer> numberSet = numberStream.collect(Collectors.toSet());
Step 3: Display the Set
The resulting set of integers is printed:
System.out.println(numberSet);
Example 2: Convert Stream of Strings to a Set
import java.util.stream.Stream;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamOfStringsToSet {
public static void main(String[] args) {
// Step 1: Create a stream of strings
Stream<String> fruitStream = Stream.of("Apple", "Banana", "Orange", "Mango", "Apple");
// Step 2: Convert the stream to a set
Set<String> fruitSet = fruitStream.collect(Collectors.toSet());
// Step 3: Display the set
System.out.println(fruitSet); // Output: [Apple, Banana, Mango, Orange]
}
}
Output
[Apple, Banana, Mango, Orange]
Explanation
Step 1: Create a Stream
We create a stream of strings that contains duplicates:
Stream<String> fruitStream = Stream.of("Apple", "Banana", "Orange", "Mango", "Apple");
Step 2: Convert the Stream to a Set
We convert the stream into a set using collect(Collectors.toSet())
, which automatically removes duplicates:
Set<String> fruitSet = fruitStream.collect(Collectors.toSet());
Step 3: Display the Set
We print the resulting set of unique strings:
System.out.println(fruitSet);
Example 3: Convert Filtered Stream to a Set
In this example, we filter the stream of numbers to retain only even numbers and then collect the result into a set.
import java.util.stream.Stream;
import java.util.Set;
import java.util.stream.Collectors;
public class FilteredStreamToSet {
public static void main(String[] args) {
// Step 1: Create a stream of integers
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Step 2: Filter and convert the stream to a set of even numbers
Set<Integer> evenNumbers = numberStream
.filter(n -> n % 2 == 0) // Filter even numbers
.collect(Collectors.toSet()); // Collect into a set
// Step 3: Display the set of even numbers
System.out.println(evenNumbers); // Output: [2, 4, 6, 8, 10]
}
}
Output
[2, 4, 6, 8, 10]
Explanation
Step 1: Create a Stream
We create a stream of integers from 1 to 10:
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Step 2: Filter the Stream and Convert to a Set
We filter the stream to retain only even numbers and collect them into a set:
Set<Integer> evenNumbers = numberStream
.filter(n -> n % 2 == 0)
.collect(Collectors.toSet());
Step 3: Display the Set
The filtered set of even numbers is printed:
System.out.println(evenNumbers);
Conclusion
In Java 8, converting a stream to a set is simple and efficient using the collect(Collectors.toSet())
method. This approach is useful when you want to collect unique elements from the stream, as the set automatically handles duplicate removal. Whether you are working with numbers, strings, or custom objects, this method is a great way to handle collections in a functional manner.
Comments
Post a Comment
Leave Comment