Convert Map to a List in Java 8 and above
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class Main { // Generic method to convert `Map<K, V>` to a list of `<Map.Entry<K, V>` public static<K, V> List<Map.Entry<K, V>> convertToList(Map<K, V> map) { return map.entrySet() .stream() .collect(Collectors.toList()); } public static void main(String[] args) { Map<String, Integer> asciiMap = new HashMap<>(); asciiMap.put("A", 65); asciiMap.put("B", 66); asciiMap.put("C", 67); // list to store mappings List<Map.Entry<String, Integer>> entries = convertToList(asciiMap); System.out.println(entries); } } |
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:
|
1 2 3 4 |
// Generic method to convert `Map<K, V>` to a list of `<Map.Entry<K, V>` public static<K, V> List<Map.Entry<K, V>> convertToList(Map<K, V> map) { return new ArrayList<>(map.entrySet()); } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; class Main { // Generic method to convert `Map<K, V>` to a `List<K>` public static<K, V> List<K> convertToList(Map<K, V> map) { // create an empty list to store keys List<K> key = new ArrayList<>(); map.entrySet().stream().forEach(entry -> { key.add(entry.getKey()); }); return key; } public static void main(String[] args) { Map<String, Integer> asciiMap = new HashMap<>(); asciiMap.put("A", 65); asciiMap.put("B", 66); asciiMap.put("C", 67); // list to store map keys List<String> keys = convertToList(asciiMap); System.out.println(keys); } } |
Output:
[A, B, C]
Here’s a better way to do it in Java 8 and above using Map.keySet():
|
1 2 3 4 5 6 |
// Generic method to convert `Map<K, V>` to a `List<K>` public static<K, V> List<K> convertToList(Map<K, V> map) { return map.keySet().stream() .collect(Collectors.toList()); } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; class Main { // Generic method to convert `Map<K, V>` to a `List<V>` public static<K, V> List<V> convertToList(Map<K, V> map) { // create an empty list to store values List<V> values = new ArrayList<>(); map.entrySet().stream().forEach(entry -> { values.add(entry.getValue()); }); return values; } public static void main(String[] args) { Map<String, Integer> asciiMap = new HashMap<>(); asciiMap.put("A", 65); asciiMap.put("B", 66); asciiMap.put("C", 67); // list to store map values List<Integer> values = convertToList(asciiMap); System.out.println(values); } } |
Output:
[65, 66, 67]
Here’s a better way to do it in Java 8 and above using Map.values():
|
1 2 3 4 5 6 |
// Generic method to convert `Map<K, V>` to a `List<V>` public static<K, V> List<V> convertToList(Map<K, V> map) { return map.values().stream() .collect(Collectors.toList()); } |
That’s all about converting Map to a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)