In this article we will see how to convert Map into List using Java 8 ? While working with Java applications you might come up with a situation where you want to convert Map into List.
Here in our example we will convert Map<Integer, String> into List<Integer> and List<String>. So basically we will get two lists out of this Map. One for keys and other is for values.
Below is the example.
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class MapToList { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "John"); map.put(2, "Raj"); map.put(3, "Tom"); List<Integer> intValues = map.keySet().stream().collect(Collectors.toList()); System.out.println(intValues); List<String> strValues = map.values().stream().collect(Collectors.toList()); System.out.println(strValues); } }
0 Comments