Program to convert a Set to Stream in Java using Generics Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 // Java Program to convert // Set to Stream in Java 8 import java.util.*; import java.util.stream.*; import java.util.function.*; class GFG { // Generic function to convert a set to stream private static <T> Stream<T> convertSetToStream(Set<T> set) { return set.stream(); } // Main method public static void main(String args[]) { // Create a set of String Set<Integer> setOfInteger = new HashSet<>( Arrays.asList(2, 4, 6, 8, 10)); // Print the set of Integer System.out.println("Set of Integer: " + setOfInteger); // Convert Set of Stream Stream<Integer> streamOfInteger = convertSetToStream(setOfInteger); // Print the Stream of Integer System.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 // Java Program to convert // Set to Stream in Java 8 import java.util.*; import java.util.stream.*; import java.util.function.*; class GFG { // Generic function to convert a set to stream private static <T> Stream<T> convertSetToStream(Set<T> set, Predicate<T> predicate) { return set.stream() .filter(predicate); } // Main method public static void main(String args[]) { // Create a set of String Set<String> setOfString = new HashSet<>( Arrays.asList("GeeksForGeeks", "A computer portal", "for", "Geeks")); // Print the set of String System.out.println("Set of String: " + setOfString); // Create the predicate for item starting with G Predicate<String> predicate = new Predicate<String>() { @Override public boolean test(String s) { // filter items that start with "G" return s.startsWith("G"); } }; // Convert Set of Stream Stream<String> streamOfString = convertSetToStream(setOfString, predicate); // Print the filter Stream of String System.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] Create Quiz Comment R RishabhPrabhu Follow 0 Improve R RishabhPrabhu Follow 0 Improve Article Tags : Misc Java java-stream java-set Java-Stream-programs Java-Set-Programs +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like