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

1. Converting Map<K,V> to List<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 list of key-value pairs by converting it to stream, as shown below:

Download  Run Code

Output:

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

 
Here’s an even shorter version of the above code that makes use of the ArrayList constructor to create a list of Map.Entry<K,V> objects containing both key and value:

Download  Run Code

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

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

Download  Run Code

Output:

[A, B, C]

 
Here’s a better way to do it in Java 8 and above using Map.keySet():

Download  Run Code

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

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

Download  Run Code

Output:

[65, 66, 67]

 
Here’s a better way to do it in Java 8 and above using Map.values():

Download  Run Code

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