Java ProgramsCollectionsConvert Map Keys to List

Convert Map Keys to List in Java

beginner·  Collections  ·  Map

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.

Input
put(Apple, 5), put(Banana, 2), put(Cherry, 3)
Output
[Apple, Banana, Cherry]

Java Program

Java
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

[Apple, Banana, Cherry]

Core Logic

Passing keySet()'s result straight into a new ArrayList's constructor copies every key into a genuinely independent list in one step.

How It Works
  1. 1scores.keySet() returns a Set view backed directly by the map — it isn't a List, and changes to it would actually modify the map itself.
  2. 2new ArrayList<>(scores.keySet()) copies those keys into a brand-new, independent List<String>, leaving the original map untouched.
  3. 3A LinkedHashMap is used here specifically so the printed key order is fully predictable — insertion order — for this example; the same keySet()-to-ArrayList technique works on any Map implementation.
  4. 4The resulting List can be sorted, indexed, or passed anywhere a List is expected, none of which keySet()'s own Set view supports directly.
For a map holding 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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Every one of the map's n keys is copied once into the new list.

Key Concepts

keySet()List constructorMap

Approach 2: Java 8

Java
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

[Apple, Banana, Cherry]

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.

How It Works
  1. 1scores.keySet() returns the same live Set view of the map's keys as the primary approach.
  2. 2.stream() opens a stream over that view.
  3. 3.collect(Collectors.toList()) gathers the streamed keys into a new, independent List.
  4. 4The result is equivalent to new ArrayList<>(scores.keySet()) — a fresh copy detached from the map.
Streaming and collecting the keys of a map holding 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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Streaming and collecting still visits each of the map's n keys exactly once, the same cost as the constructor form.

Key Concepts

StreamCollectors.toList()

Related Programs