Convert Stream to Set in Java
Below given are some methods which can be used to convert Stream to Set in Java.
Method 1 : Using Collectors
Stream collect() method takes elements from a stream and stores them in a collection.collect(Collector.toSet()) collects elements from a stream to a Set.
Stream.collect() method can be used to collect elements of a stream in a container. The Collector which is returned by Collectors.toSet() can be passed that accumulates the elements of stream into a new Set.
// Java code for converting // Stream to Set using Collectors import java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Integers Stream<Integer> stream = Stream.of(-2, -1, 0, 1, 2); // Using Stream.collect() to collect the // elements of stream in a container. Set<Integer> streamSet = stream.collect(Collectors.toSet()); // Displaying the elements streamSet.forEach(num -> System.out.println(num)); } } |
-1 0 -2 1 2
Method 2 : Converting Stream to Array and then to Set
The problem of converting Stream into Set can be divided into two parts :
1) Convert Stream to an Array 2) Convert Array to a Set
// Java code for converting // Stream to Set using Divide // and Conquer import java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of("G", "E", "K", "S"); // Converting Stream into an Array String[] arr = stream.toArray(String[] :: new); // Creating a HashSet Set<String> set = new HashSet<>(); // Converting Array to set Collections.addAll(set,arr); // Displaying the elements set.forEach(str -> System.out.println(str)); } } |
S E G K
Note : Output is random because HashSet takes input in random order as generated hash value.
Method 3 : Using forEach
Stream can be converted into Set using forEach(). Loop through all elements of the stream using forEach() method and then use set.add() to add each elements into an empty set.
// Java code for converting // Stream to Set using forEach import java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Integers Stream<Integer> stream = Stream.of(5, 10, 15, 20, 25); // Creating a HashSet Set<Integer> set = new HashSet<>(); // using set.add() to add elements // of stream into empty set stream.forEach(set::add); // Displaying the elements set.forEach(num -> System.out.println(num)); } } |
20 5 25 10 15
Note : If Stream is parallel, then elements may not be processed in original order using forEach() method. To preserve the original order, forEachOrdered() method is used.
Convert a Set to Stream in Java
Recommended Posts:
- Convert a Set to Stream in Java
- Convert an Iterable to Stream in Java
- Convert an Iterator to Stream in Java
- Program to convert a Map to a Stream in Java
- Program to Convert List to Stream in Java
- Program to Convert Stream to an Array in Java
- Program to convert a Set to Stream in Java using Generics
- Program to convert Primitive Array to Stream in Java
- Program to convert Boxed Array to Stream in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- Character Stream Vs Byte Stream in Java
- Java Stream | Collectors toCollection() in Java
- Java 8 Stream
- Stream In Java
- Stream map() in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



