How do I convert a Map to List in Java?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How do I convert a numpy array to and display an image?
- How do I convert a Swift Array to a String?
- How do I convert a Swift Array to a String?
- How do I convert all of the items in a list to floats?
- How do I convert a String to an InputStream in Java?
- How do I convert an OutputStream to an InputStream?
- How do I convert all strings in a list of lists to integers?
- How do I convert an array of floats to a byte and back?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.