Convert Map Keys to List in Java
Problem
A Map's keySet() returns a live view of its keys, not an independent List — wrapping it in a new ArrayList copies those keys out into a separate, ordinary list.
Given a Map, produce a List containing all of its keys.
Java Program
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ConvertMapKeysToList {
public static void main(String[] args) {
Map<String, Integer> scores = new LinkedHashMap<>();
scores.put("Apple", 5);
scores.put("Banana", 2);
scores.put("Cherry", 3);
List<String> keys = new ArrayList<>(scores.keySet()); // copies the keys into an independent list
System.out.println(keys);
}
}Output
Core Logic
Passing keySet()'s result straight into a new ArrayList's constructor copies every key into a genuinely independent list in one step.
- 1
scores.keySet()returns aSetview backed directly by the map — it isn't a List, and changes to it would actually modify the map itself. - 2
new ArrayList<>(scores.keySet())copies those keys into a brand-new, independentList<String>, leaving the original map untouched. - 3A
LinkedHashMapis used here specifically so the printed key order is fully predictable — insertion order — for this example; the samekeySet()-to-ArrayListtechnique works on any Map implementation. - 4The resulting List can be sorted, indexed, or passed anywhere a List is expected, none of which keySet()'s own Set view supports directly.
Apple, Banana, and Cherry as keys, the resulting list is [Apple, Banana, Cherry], in the order they were inserted.Key Point: keySet() itself is a live, connected view of the map's keys, not a copy — wrapping it in new ArrayList<>(...) is what actually detaches the keys into their own independent list.
Why: Every one of the map's n keys is copied once into the new list.
Key Concepts
Approach 2: Java 8
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ConvertMapKeysToListStream {
public static void main(String[] args) {
Map<String, Integer> scores = new LinkedHashMap<>();
scores.put("Apple", 5);
scores.put("Banana", 2);
scores.put("Cherry", 3);
// Streams the live keySet() view, collecting into a new independent list
List<String> keys = scores.keySet().stream().collect(Collectors.toList());
System.out.println(keys);
}
}
Output
Core Logic
Streaming keySet() and collecting it into a List expresses the same conversion as a pipeline, which reads naturally when the keys are being filtered or transformed along the way.
- 1
scores.keySet()returns the same live Set view of the map's keys as the primary approach. - 2
.stream()opens a stream over that view. - 3
.collect(Collectors.toList())gathers the streamed keys into a new, independent List. - 4The result is equivalent to
new ArrayList<>(scores.keySet())— a fresh copy detached from the map.
Apple, Banana, and Cherry produces the same [Apple, Banana, Cherry] list.Key Point: For a plain, unfiltered conversion like this one, new ArrayList<>(scores.keySet()) is shorter and equally clear — the stream form earns its keep once a .filter() or .map() joins the same pipeline.
Why: Streaming and collecting still visits each of the map's n keys exactly once, the same cost as the constructor form.