This post will discuss how to convert the map to a stream in Java.

1. Converting Map<K,V> to Stream<Map.Entry<K,V>>

We know that Map.entrySet() returns a set view of the mappings contained in this map. In Java 8, we can easily get a stream of key-value pairs, as shown below:

Download  Run Code

Output:

[A=65, B=66, C=67]

2. Converting Map<K,V> to Stream<K>

We can get a stream of keys of the map using Map.keySet() in Java 8 and above:

Download  Run Code

Output:

[A, B, C]

3. Converting Map<K,V> to Stream<V>

We can get a stream of values of the map using Map.values() in Java 8 and above:

Download  Run Code

Output:

[65, 66, 67]

That’s all about converting Map to a Stream in Java.