Java 8 – Convert Map to List
Few Java examples to convert a Map
to a List
Map<String, String> map = new HashMap<>();
// Convert all Map keys to a List
List<String> result = new ArrayList(map.keySet());
// Convert all Map values to a List
List<String> result2 = new ArrayList(map.values());
// Java 8, Convert all Map keys to a List
List<String> result3 = map.keySet().stream()
.collect(Collectors.toList());
// Java 8, Convert all Map values to a List
List<String> result4 = map.values().stream()
.collect(Collectors.toList());
// Java 8, seem a bit long, but you can enjoy the Stream features like filter and etc.
List<String> result5 = map.values().stream()
.filter(x -> !"apple".equalsIgnoreCase(x))
.collect(Collectors.toList());
// Java 8, split a map into 2 List, it works!
// refer example 3 below
1. Map To List
For a simple Map
to List
conversion, just uses the below code :
ConvertMapToList.java
package com.mkyong;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertMapToList {
public static void main(String[] args) {
Map<Integer, String> 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<Integer> result = new ArrayList(map.keySet());
result.forEach(System.out::println);
System.out.println("\n2. Export Map Value to List...");
List<String> result2 = new ArrayList(map.values());
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
Java 8 – Map To List
For Java 8, you can convert the Map
into a stream, process it and returns it back as a List
ConvertMapToList.java
package com.mkyong;
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<Integer, String> 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<Integer> result = map.keySet().stream()
.collect(Collectors.toList());
result.forEach(System.out::println);
System.out.println("\n2. Export Map Value to List...");
List<String> result2 = map.values().stream()
.collect(Collectors.toList());
result2.forEach(System.out::println);
System.out.println("\n3. Export Map Value to List..., say no to banana");
List<String> result3 = map.keySet().stream()
.filter(x -> !"banana".equalsIgnoreCase(x))
.collect(Collectors.toList());
result3.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
3. Export Map Value to List..., say no to banana
dragonfruit
orange
watermelon
apple
3. Java 8 – Convert Map into 2 List
This example is a bit extreme, uses map.entrySet()
to convert a Map
into 2 List
ConvertMapToList.java
package com.mkyong;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
//https://www.mkyong.com/java8/java-8-how-to-sort-a-map/
public class ConvertMapToList {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
// split a map into 2 List
List<Integer> resultSortedKey = new ArrayList<>();
List<String> resultValues = map.entrySet().stream()
//sort a Map by key and stored in resultSortedKey
.sorted(Map.Entry.<Integer, String>comparingByKey().reversed())
.peek(e -> resultSortedKey.add(e.getKey()))
.map(x -> x.getValue())
// filter banana and return it to resultValues
.filter(x -> !"banana".equalsIgnoreCase(x))
.collect(Collectors.toList());
resultSortedKey.forEach(System.out::println);
resultValues.forEach(System.out::println);
}
}
Output
//resultSortedKey
50
40
30
20
10
//resultValues
dragonfruit
watermelon
orange
apple
In Java8 Map to List example, the below code snippet needs to change. Instead of map.keySet(), it should be map.values().
System.out.println(“n3. Export Map Value to List…, say no to banana”);
List result3 = map.keySet().stream()
.filter(x -> !”banana”.equalsIgnoreCase(x))
.collect(Collectors.toList());
result3.forEach(System.out::println);
public class Test {
public static void main(String[] args) {
TreeMap hm=new TreeMap();
hm.put(3, “arun singh”);
hm.put(5, “vinay singh”);
hm.put(1, “bandagi singh”);
hm.put(6, “vikram singh”);
hm.put(2, “panipat singh”);
hm.put(28, “jakarta singh”);
ArrayList al=new ArrayList(hm.values());
Collections.sort(al, new myComparator());
System.out.println(“//sort by values n”);
for(String obj: al){
for(Map.Entry map2:hm.entrySet()){
if(map2.getValue().equals(obj)){
System.out.println(map2.getKey()+” “+map2.getValue());
}
}
}
}
}
class myComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
String o3=(String) o1;
String o4 =(String) o2;
return o3.compareTo(o4);
}
}
OUTPUT=
//sort by values
3 arun singh
1 bandagi singh
28 jakarta singh
2 panipat singh
6 vikram singh
5 vinay singh
why are you recommending what’s probably the most verbose and slowest way possible?, map.keySet() returns a Set fand map.values() return a Collection, though I’m not sure what type of Collection, and that may very. so new ArrayList( map.keySet() ) or new ArrayList( map.values), I mean technically even map.keySet().stream().collect( Collectors.toList() ). The only reason to use an entrySet is if you don’t want to iterate over them twice, and you aren’t showing how to take a map and Partition it in to 2 lists, you can do that, but I’d have to look it up.
Sorry, article is updated with more proper structure to show the differences. Initially, I just want to show how Java 8 can do the same stuff, but it bring confusion to readers.
Sorry, and thanks for your suggestions, hope you like the updated article.