Last Updated : 11 Jul, 2025
A
Streamis 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:
// Java Program to convert
// Map<Key, Value> into Stream
import java.util.*;
import java.util.stream.*;
class GFG {
// Generic function to convert List of
// String to List of Integer
public static <K, V> Stream<Map.Entry<K, V> >
convertMapToStream(Map<K, V> map)
{
// Return the obtained Stream
return map
// Convert the Map to Set
.entrySet()
// Convert the Set to Stream
.stream();
}
public static void main(String args[])
{
// Create a Map
Map<Integer, String> map = new HashMap<>();
// Add entries to the Map
map.put(1, "Geeks");
map.put(2, "forGeeks");
map.put(3, "A computer Portal");
// Print the Map
System.out.println("Map: " + map);
// Convert the Map to Stream
Stream<Map.Entry<Integer, String> > stream =
convertMapToStream(map);
// Print the TreeMap
System.out.println("Stream: "
+ Arrays.toString(stream.toArray()));
}
}
Output:
Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [1=Geeks, 2=forGeeks, 3=A computer Portal]
// Java Program to convert
// Map<Key, Value> into Stream
import java.util.*;
import java.util.stream.*;
class GFG {
// Generic function to convert List of
// String to List of Integer
public static <K, V> Stream<K>
convertMapToStream(Map<K, V> map)
{
// Return the obtained Stream
return map
// Convert the Map to Set<Key>
.keySet()
// Convert the Set to Stream
.stream();
}
public static void main(String args[])
{
// Create a Map
Map<Integer, String> map = new HashMap<>();
// Add entries to the Map
map.put(1, "Geeks");
map.put(2, "forGeeks");
map.put(3, "A computer Portal");
// Print the Map
System.out.println("Map: " + map);
// Convert the Map to Stream
Stream<Integer> stream = convertMapToStream(map);
// Print the TreeMap
System.out.println("Stream: "
+ Arrays.toString(stream.toArray()));
}
}
Output:
Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [1, 2, 3]
// Java Program to convert
// Map<Key, Value> into Stream
import java.util.*;
import java.util.stream.*;
class GFG {
// Generic function to convert List of
// String to List of Integer
public static <K, V> Stream<V>
convertMapToStream(Map<K, V> map)
{
// Return the obtained Stream
return map
// Convert the Map to Set<Value>
.values()
// Convert the Set to Stream
.stream();
}
public static void main(String args[])
{
// Create a Map
Map<Integer, String> map = new HashMap<>();
// Add entries to the Map
map.put(1, "Geeks");
map.put(2, "forGeeks");
map.put(3, "A computer Portal");
// Print the Map
System.out.println("Map: " + map);
// Convert the Map to Stream
Stream<String> stream = convertMapToStream(map);
// Print the TreeMap
System.out.println("Stream: "
+ Arrays.toString(stream.toArray()));
}
}
Output:
Map: {1=Geeks, 2=forGeeks, 3=A computer Portal} Stream: [Geeks, forGeeks, A computer Portal]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4