Program to convert a Map to a Stream in Java
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Below are various method to convert Map to Stream in Java:
- Converting complete Map<Key, Value> into Stream: This can be done with the help of Map.entrySet() method which returns a Set view of the mappings contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set.stream() method.
Algorithm:
- Get the Map<Key, Value>.
- Convert Map<Key, Value> into Set<Key> using Map.entrySet() method.
- Convert the obtained Set into Stream using Set.stream()
- Return/Print the Stream of Map.
Program:
// Java Program to convert// Map<Key, Value> into Streamimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert List of// String to List of Integerpublicstatic<K, V> Stream<Map.Entry<K, V> >convertMapToStream(Map<K, V> map){// Return the obtained Streamreturnmap// Convert the Map to Set.entrySet()// Convert the Set to Stream.stream();}publicstaticvoidmain(String args[]){// Create a MapMap<Integer, String> map =newHashMap<>();// Add entries to the Mapmap.put(1,"Geeks");map.put(2,"forGeeks");map.put(3,"A computer Portal");// Print the MapSystem.out.println("Map: "+ map);// Convert the Map to StreamStream<Map.Entry<Integer, String> > stream =convertMapToStream(map);// Print the TreeMapSystem.out.println("Stream: "+ Arrays.toString(stream.toArray()));}}chevron_rightfilter_noneOutput:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [1=Geeks, 2=forGeeks, 3=A computer Portal] - Converting only the Keyset of the Map<Key, Value> into Stream: This can be done with the help of Map.keySet() method which returns a Set view of the keys contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set.stream() method.
Algorithm:
- Get the Map<Key, Value>.
- Convert Map<Key, Value> into Set<Key> using Map.keySet() method.
- Convert the obtained Set into Stream using Set.stream()
- Return/Print the Stream of Map.
Program:
// Java Program to convert// Map<Key, Value> into Streamimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert List of// String to List of Integerpublicstatic<K, V> Stream<K>convertMapToStream(Map<K, V> map){// Return the obtained Streamreturnmap// Convert the Map to Set<Key>.keySet()// Convert the Set to Stream.stream();}publicstaticvoidmain(String args[]){// Create a MapMap<Integer, String> map =newHashMap<>();// Add entries to the Mapmap.put(1,"Geeks");map.put(2,"forGeeks");map.put(3,"A computer Portal");// Print the MapSystem.out.println("Map: "+ map);// Convert the Map to StreamStream<Integer> stream = convertMapToStream(map);// Print the TreeMapSystem.out.println("Stream: "+ Arrays.toString(stream.toArray()));}}chevron_rightfilter_noneOutput:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [1, 2, 3] - Converting only the Value of the Map<Key, Value> into Stream: This can be done with the help of Map.values() method which returns a Set view of the values contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set.stream() method.
Algorithm:
- Get the Map<Key, Value>.
- Convert Map<Key, Value> into Set<Value> using Map.values() method.
- Convert the obtained Set into Stream using Set.stream()
- Return/Print the Stream of Map.
Program:
// Java Program to convert// Map<Key, Value> into Streamimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert List of// String to List of Integerpublicstatic<K, V> Stream<V>convertMapToStream(Map<K, V> map){// Return the obtained Streamreturnmap// Convert the Map to Set<Value>.values()// Convert the Set to Stream.stream();}publicstaticvoidmain(String args[]){// Create a MapMap<Integer, String> map =newHashMap<>();// Add entries to the Mapmap.put(1,"Geeks");map.put(2,"forGeeks");map.put(3,"A computer Portal");// Print the MapSystem.out.println("Map: "+ map);// Convert the Map to StreamStream<String> stream = convertMapToStream(map);// Print the TreeMapSystem.out.println("Stream: "+ Arrays.toString(stream.toArray()));}}chevron_rightfilter_noneOutput:Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [Geeks, forGeeks, A computer Portal]
Recommended Posts:
- Program to Convert List to Stream in Java
- Program to convert a Set to Stream in Java using Generics
- Program to Convert Stream to an Array in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Primitive Array to Stream in Java
- Convert a Set to Stream in Java
- Convert Stream to Set in Java
- Convert an Iterator to Stream in Java
- Convert an Iterable to Stream in Java
- Program to Iterate over a Stream with Indices in Java 8
- Difference between Stream.of() and Arrays.stream() method in Java
- Character Stream Vs Byte Stream in Java
- Program to Convert Set to List in Java
- Program to convert Array to Set in Java
- Program to Convert List to Map in Java
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.

