In this article we will see how to convert a map to list in java 8, there are various available ways to do so in java 8, some of them are shown below:
class Employee {
	private int id;
	private String name;

	// Getter Setters, constructor and toString()

}



1) Convert map to list using keys

		Map<Integer, Employee> employeesMap = new HashMap<>();
		employeesMap.put(3, new Employee(3, "Zenny"));
		employeesMap.put(1, new Employee(1, "John"));
		employeesMap.put(2, new Employee(2, "Ben"));

		List<Integer> keysList = employeesMap.keySet().stream().collect(Collectors.toList());
		keysList.forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:


1,2,3


2) Convert map to list using values

		Map<Integer, Employee> employeesMap = new HashMap<>();
		employeesMap.put(3, new Employee(3, "Zenny"));
		employeesMap.put(1, new Employee(1, "John"));
		employeesMap.put(2, new Employee(2, "Ben"));

		List<Employee> keysList = employeesMap.values().stream().collect(Collectors.toList());

		keysList.forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:


Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=3, name=Zenny]
  • By Techburps.com
  • Oct 6, 2018
  • Java 8