Convert a Set to a Stream in Java
This post will discuss how to convert a set to stream in Java 8 and above. We will also learn how to apply a filter on a stream and convert it back to a set or list.
1. Convert a set to stream
Converting a set to stream is very simple. Set interface extends Collection interface and Collection has stream() method that returns a sequential stream of the collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; class Main { // Generic method to convert a set to stream private static <T> Stream<T> setToStream (Set<T> set) { return set.stream(); } // Program to convert a set to stream in Java 8 and above public static void main(String[] args) { Set<Integer> set = new HashSet<>(); for (int i = 0; i < 5; i++) { set.add(i + 1); } Stream<Integer> stream = setToStream(set); System.out.println(Arrays.toString(stream.toArray())); } } |
Output:
[1, 2, 3, 4, 5]
2. Filter stream using a predicate
For filtering a stream, we can use filter(Predicate) that returns a stream consisting of elements that match the given predicate.
In the following example, we’ll create a stream of Integer objects using Collection.stream() and filter it to produce a stream containing only even numbers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; class Main { // Program to convert a set to stream and filter it in Java 8 and above public static void main(String[] args) { Set<Integer> set = new HashSet<>(); for (int i = 0; i < 5; i++) { set.add(i + 1); } Predicate<Integer> removeOdd = new Predicate<Integer>() { @Override public boolean test(Integer i) { return i % 2 == 0; // remove odd numbers } }; set.stream() .filter(removeOdd) .forEach(System.out::println); } } |
Output:
2
4
We can also specify a lambda expression or method reference in place of the predicate:
|
1 2 3 |
set.stream() .filter(n -> n % 2 == 0) // print only even numbers .forEach(System.out::println); |
3. Convert stream back to set
We can accumulate the stream elements into a new set using a Collector returned by Collectors.toList().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { // Generic method to convert a set to stream private static <T> Stream<T> setToStream (Set<T> set) { return set.stream(); } // Generic method to convert a stream to a set private static <T> Set<T> streamToSet (Stream<T> stream) { return stream .filter(Objects::nonNull) // optionally apply any filter .collect(Collectors.toSet()); } // Program to convert the stream to set in Java 8 and above public static void main(String[] args) { Set<Integer> set = new HashSet<>(); for (int i = 0; i < 5; i++) { set.add(i + 1); } // convert a set to stream Stream<Integer> stream = setToStream(set); // convert stream back to set set = streamToSet(stream); System.out.println(set); } } |
Output:
[1, 2, 3, 4, 5]
We can also accumulate the stream elements in a new list using Collectors.toList(), as shown below:
|
1 2 3 4 5 6 7 |
// Generic method to convert a stream to a list private static <T> List<T> streamToList (Stream<T> stream) { return stream .filter(Objects::nonNull) // optionally apply any filter .collect(Collectors.toList()); } |
That’s all about converting a Set to Stream in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)