A Java 8 example to convert a Map to a List
ConvertMapToList.java
package com.mkyong.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ConvertMapToList {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
System.out.println("\n1. Export Map Key to List...");
List result = map.entrySet().stream()
.map(x -> x.getKey())
.collect(Collectors.toList());
result.forEach(System.out::println);
System.out.println("\n2. Export Map Value to List...");
List result2 = map.entrySet().stream()
.map(x -> x.getValue())
.collect(Collectors.toList());
result2.forEach(System.out::println);
}
}
Output
1. Export Map Key to List...
50
20
40
10
30
2. Export Map Value to List...
dragonfruit
orange
watermelon
apple
banana
References