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 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]
- 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 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]
- 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 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]