Program to convert a Set to Stream in Java using Generics
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element.
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Below are the various methods to convert Set to Stream.
- Using Collection.stream(): This method involves directly converting the Set to Stream using Collection.stream() method.
Algorithm:
- Get the Set to be converted.
- Convert Set to Stream. This is done using Set.stream().
- Return/Print the Stream.
// Java Program to convert// Set to Stream in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.*;classGFG {// Generic function to convert a set to streamprivatestatic<T> Stream<T> convertSetToStream(Set<T> set){returnset.stream();}// Main methodpublicstaticvoidmain(String args[]){// Create a set of StringSet<Integer> setOfInteger =newHashSet<>(Arrays.asList(2,4,6,8,10));// Print the set of IntegerSystem.out.println("Set of Integer: "+ setOfInteger);// Convert Set of StreamStream<Integer>streamOfInteger = convertSetToStream(setOfInteger);// Print the Stream of IntegerSystem.out.println("Stream of Integer: "+ Arrays.toString(streamOfInteger.toArray()));}}Output:Set of Integer: [2, 4, 6, 8, 10] Stream of Integer: [2, 4, 6, 8, 10]
- Using Predicate to filter the Stream: In this method filter(Predicate) method is used that returns a stream consisting of elements that match the given predicate condition. The Functional Interface Predicate is defined in the java.util.Function package and can therefore be used as the assignment target for a lambda expression or method reference. It improves manageability of code, helps in unit-testing them separately
Algorithm:
- Get the Set to be converted.
- Define the Predicate condition by either using pre-defined static methods or by creating a new method by overriding the Predicate interface. In this program, the interface is overridden to match the strings that start with “G”.
- Convert Set to Stream. This is done using Set.stream().
- Filter the obtained stream using the defined predicate condition
- The required Stream has been obtained. Return/Print the Stream.
// Java Program to convert// Set to Stream in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.*;classGFG {// Generic function to convert a set to streamprivatestatic<T> Stream<T>convertSetToStream(Set<T> set, Predicate<T> predicate){returnset.stream().filter(predicate);}// Main methodpublicstaticvoidmain(String args[]){// Create a set of StringSet<String>setOfString =newHashSet<>(Arrays.asList("GeeksForGeeks","A computer portal","for","Geeks"));// Print the set of StringSystem.out.println("Set of String: "+ setOfString);// Create the predicate for item starting with GPredicate<String> predicate =newPredicate<String>() {@Overridepublicbooleantest(String s){// filter items that start with "G"returns.startsWith("G");}};// Convert Set of StreamStream<String>streamOfString = convertSetToStream(setOfString, predicate);// Print the filter Stream of StringSystem.out.println("Stream from List with items"+" starting with G: ");System.out.println(Arrays.toString(streamOfString.toArray()));}}Output:Set of String: [for, Geeks, GeeksForGeeks, A computer portal] Stream from List with items starting with G: [Geeks, GeeksForGeeks]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


