collections

Map to List Conversion in Java

🔹 Converting a Map to a List (Deep Dive, Interview-Ready)

A Map maps keys to values, but sometimes you need to process that data sequentially, sort it, or pass it to an API that only accepts a List. In Java, you cannot convert a Map directly into a single List because a Map holds key-value pairs.

Instead, you must extract either the Keys, the Values, or the Entries (both).


📌 1. The Standard Java Approach (Pre-Java 8)

The absolute cleanest and most performant way to extract lists from a Map is by passing the Map's Collection views directly into an ArrayList constructor.

🔸 1. Converting Keys to a List

Map<Integer, String> map = new HashMap<>(); // ... populate map ... // Extract all keys List<Integer> keyList = new ArrayList<>(map.keySet());

🔸 2. Converting Values to a List

// Extract all values List<String> valueList = new ArrayList<>(map.values());

🔸 3. Converting Entries to a List

This is extremely useful when you need to keep the key and value together, usually so you can sort them.

// Extract Key-Value pairs List<Map.Entry<Integer, String>> entryList = new ArrayList<>(map.entrySet());

📌 2. The Modern Java Approach (Java 8 Streams)

While the constructor approach above is faster and simpler, the Stream API allows you to extract and transform or filter the data in a single fluent pipeline.

🔸 Filtering before collecting

// Get a list of values, but only for keys greater than 100 List<String> filteredValues = map.entrySet().stream() .filter(entry -> entry.getKey() > 100) .map(Map.Entry::getValue) .collect(Collectors.toList());

🔸 Sorting the Map by Value

Maps are generally unordered. The most common interview question involving Map-to-List conversion is: "How do you sort a HashMap by its values?"

The Solution: Convert the entrySet to a List, sort the list, and then (optionally) put it into a LinkedHashMap to preserve the new order.

List<Map.Entry<Integer, String>> list = new ArrayList<>(map.entrySet()); // Sort the list by the map's Values list.sort(Map.Entry.comparingByValue()); // Iterate over the newly sorted list for (Map.Entry<Integer, String> entry : list) { System.out.println(entry.getKey() + ": " + entry.getValue()); }

📌 3. Extracting Both into a Single List of Custom Objects

Sometimes, Map.Entry is too generic. You want to map the entries directly into your own custom DTO (Data Transfer Object).

public class UserDto { int id; String name; // constructor... } // Convert Map<Integer, String> directly into List<UserDto> List<UserDto> userList = map.entrySet().stream() .map(entry -> new UserDto(entry.getKey(), entry.getValue())) .collect(Collectors.toList());

🔥 Interview Gold Statement

"To convert a Map to a List, you must decide whether you want a list of Keys, Values, or Entries. For simple, raw extraction, passing map.keySet(), map.values(), or map.entrySet() directly into the ArrayList constructor is the most memory-efficient and idiomatic approach. However, if the conversion requires filtering, sorting, or mapping the entries into Custom DTOs, I immediately reach for the Java 8 Stream API to process the entrySet in a fluent pipeline."


âš¡ Final Verdict

  • ✅ Use new ArrayList<>(map.values()) for raw, immediate extraction.
  • 🎯 Use map.entrySet().stream() when you need to filter, sort, or map the data during the conversion process.
Map to List Conversion in Java | DevExCode