Convert Map Values to List in Java
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.
Java Program
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
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.
- 1A
LinkedHashMap<String, Integer>namedscoresis built with three entries, in a fixed insertion order. - 2
scores.values()returns aCollection<Integer>view backed by the map — it reflects the map's current values, not a separate copy yet. - 3
new ArrayList<>(scores.values())takes that view and copies every value into a brand-new, independentList. - 4The resulting
valueListno longer has any connection toscores— changing one afterward wouldn't affect the other.
{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.
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
Approach 2: Java 8
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
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.
- 1
scores.values()returns the same live Collection view of the map's values as the primary approach. - 2
.stream()opens a stream over that view. - 3
.collect(Collectors.toList())gathers the streamed values into a new, independent List. - 4The result is equivalent to
new ArrayList<>(scores.values())— a fresh copy detached from the map.
{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.
Why: Streaming and collecting still visits each of the map's n values exactly once, the same cost as the constructor form.