Java ProgramsCollectionsConvert Map Values to List

Convert Map Values to List in Java

beginner·  Collections  ·  Map

Problem

A Map's values() view returns a live collection of just its values, which can be handed straight to a List constructor to get an independent copy.

Given a Map, extract all of its values into a new List.

Input
{Alice=90, Bob=85, Carol=95}
Output
Values: [90, 85, 95]

Java Program

Java
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class MapValuesToList { public static void main(String[] args) { Map<String, Integer> scores = new LinkedHashMap<>(); scores.put("Alice", 90); scores.put("Bob", 85); scores.put("Carol", 95); List<Integer> valueList = new ArrayList<>(scores.values()); // copies the values() view into a standalone list System.out.println("Values: " + valueList); } }

Output

Values: [90, 85, 95]

Core Logic

Passing the Map's values() view straight into a new ArrayList's constructor copies every value out in one step, with no manual loop needed.

How It Works
  1. 1A LinkedHashMap<String, Integer> named scores is built with three entries, in a fixed insertion order.
  2. 2scores.values() returns a Collection<Integer> view backed by the map — it reflects the map's current values, not a separate copy yet.
  3. 3new ArrayList<>(scores.values()) takes that view and copies every value into a brand-new, independent List.
  4. 4The resulting valueList no longer has any connection to scores — changing one afterward wouldn't affect the other.
For {Alice=90, Bob=85, Carol=95}, valueList ends up holding [90, 85, 95], in the map's insertion order since it's a LinkedHashMap.
💡

Key Point: A plain HashMap would produce the same values but in no guaranteed order — using LinkedHashMap is what makes the resulting list's order predictable and match insertion order.

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

Why: Every one of the map's n values is copied once into the new list, which then holds its own n elements.

Key Concepts

Map.values()ArrayList constructorLinkedHashMap

Approach 2: Java 8

Java
import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class MapValuesToListStream { public static void main(String[] args) { Map<String, Integer> scores = new LinkedHashMap<>(); scores.put("Alice", 90); scores.put("Bob", 85); scores.put("Carol", 95); // Streams the live values() view, collecting into a new independent list List<Integer> valueList = scores.values().stream().collect(Collectors.toList()); System.out.println("Values: " + valueList); } }

Output

Values: [90, 85, 95]

Core Logic

Streaming values() and collecting it into a List expresses the same conversion as a pipeline, which reads naturally when the values are being filtered or transformed along the way.

How It Works
  1. 1scores.values() returns the same live Collection view of the map's values as the primary approach.
  2. 2.stream() opens a stream over that view.
  3. 3.collect(Collectors.toList()) gathers the streamed values into a new, independent List.
  4. 4The result is equivalent to new ArrayList<>(scores.values()) — a fresh copy detached from the map.
Streaming and collecting the values of {Alice=90, Bob=85, Carol=95} produces the same [90, 85, 95] list.
💡

Key Point: For a plain, unfiltered conversion like this one, new ArrayList<>(scores.values()) 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 values exactly once, the same cost as the constructor form.

Key Concepts

StreamCollectors.toList()

Related Programs