How do I convert a Map to List in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java, converting a Map to a List is a common task that you might encounter in various scenarios. This could be for ease of operation, API requirements, or merely data transformation purposes. A Map in Java is a data structure that stores key-value pairs, while a List is an ordered collection that stores elements. There are several methods to transform a Map into a List depending on what information from the map is required in the list (keys, values, or both).
Understanding Map and List Collections
Before delving into the conversion process, let's quickly understand the essential interfaces:
- Map:
Map<K, V>is an object that maps keys to values - without duplicate keys. Each key maps to exactly one value. The most commonly used implementations of Map interface areHashMap,TreeMap, andLinkedHashMap. - List:
List<E>is an ordered collection (also known as a sequence). Lists can contain duplicate elements. Examples includeArrayList,LinkedList, andVector.
Methods to Convert a Map to a List
1. Extracting Keys or Values Using Streams
In Java 8 and later, Streams provide a very convenient way to collect keys or values from a Map.
- To Extract Keys:
- To Extract Values:
Using streams, this can alternatively be done as:
2. Converting Key-Value Pairs to List
Sometimes, you may need to store the key-value pairs themselves in a list, often as an entry set where each element in the list is a key-value pair:
Or using a stream:
3. Custom Class to Hold Key-Value Pairs
If you need more control or additional processing, you can define a custom class to hold the key-value data:
Applications and Considerations
- Performance: Converting a map to a list, particularly with large datasets, should be considered within the context of performance. Streams provide a neat and concise approach but ensure to evaluate performance impacts, especially with complex processing inside streams.
- Order:
HashMapdoes not retain any order, whereasLinkedHashMapmaintains the insertion order. Choice ofListtype (ArrayListvs.LinkedList) should also be considered based on subsequent operations (frequent insertions, deletions, random access, etc.). - Null Values: Be mindful of null keys and values; while certain maps and lists handle nulls, others might throw exceptions.
Summary Table
| Task | Code Example |
| Extract Keys | List<K> keyList = new ArrayList<>(map.keySet()); |
| Extract Values | List<V> valuesList = new ArrayList<>(map.values()); |
| Extract Entries | List<Map.Entry<K, V>> entryList = new ArrayList<>(map.entrySet()); |
| Advanced (Custom Object) | // See the custom KeyValuePair class example above |
Conclusion
Converting a Map to a List in Java can be done in several ways depending on the requirements—either by extracting keys, values, or both. Using Java 8 Streams can simplify the code and improve readability, but always consider the implications of data size and performance.

