Convert a Map into a List of objects
A Java Map can be converted to a List of keys, values, or entries using the Stream API. This article demonstrates extracting map.keySet(), map.values(), and map.entrySet() into an ArrayList, sorting entries by key or value during the conversion, and filtering entries with a predicate before collecting them.
In this article we are going to look at different ways of converting a MapList
If you want to construct List using key set, values collection or EntrySet, you can simply create List with those values, but if you want to process the values while creating List, Stream API comes handy.
Map<Integer, Employee> employeesMap = new HashMap<>();
employeesMap.put(100, new Employee("Employee A", "IT Support"));
employeesMap.put(200, new Employee("Employee B", "Logistics"));
employeesMap.put(300, new Employee("Employee C", "Administration"));
employeesMap.put(400, new Employee("Employee D", "Cyber Security"));
List<Employee> employees = new ArrayList<>(employeesMap.values()); // List with Employee objects
List<Integer> employeeIds = new ArrayList<>(employeesMap.keySet()); // List with employee id(s)
In order to follow below examples, create a class called Employee
public class Employee {
String name;
String department;
public Employee(String name, String department) {
this.name = name;
this.department = department;
}
@Override
public String toString() {
return "[name : "+name + ", department : "+department+"]";
}
}
Using Stream API over Map's values
Let's write a program to create a List<String>Map<Integer, Employee>keyvalueEmployee
public static void convertMapValuesToList() {
Map<Integer, Employee> employeesMap = Map.of(100, new Employee("Employee A", "IT Support"),
101, new Employee("Employee B", "IT Support"),
200, new Employee("Employee C", "Logistics"),
300, new Employee("Employee D", "Administration"),
400, new Employee("Employee E", "Cyber Security")
);
List<String> employeeNames = employeesMap.values().stream().map(employee -> employee.name).collect(Collectors.toList());
System.out.println(employeeNames);
}
When you run the above program, you will get the below output.
[Employee E, Employee D, Employee C, Employee B, Employee A]
Another example, say you want to filter employees who works in IT Support,
then you can use filter()
public static void convertMapValuesToListAndFilter() {
Map<Integer, Employee> employeesMap = Map.of(100, new Employee("Employee A", "IT Support"),
101, new Employee("Employee B", "IT Support"),
200, new Employee("Employee C", "Logistics"),
300, new Employee("Employee D", "Administration"),
400, new Employee("Employee E", "Cyber Security")
);
List<String> employeeNames = employeesMap.values().stream()
.filter(employee -> "IT Support".equals(employee.department))
.map(employee -> employee.name).collect(Collectors.toList());
System.out.println(employeeNames);
}
When you run the above program, you will get the below output.
[Employee B, Employee A]
Using Stream API over Map's KeySet
Let's write a program to create a List<Integer>Map<Integer, Employee>keyvalueEmployee
public static void convertMapKeySetToList() {
Map<Integer, Employee> employeesMap = Map.of(100, new Employee("Employee A", "IT Support"),
101, new Employee("Employee B", "IT Support"),
200, new Employee("Employee C", "Logistics"),
300, new Employee("Employee D", "Administration"),
400, new Employee("Employee E", "Cyber Security")
);
List<Integer> employeeIds = employeesMap.keySet().stream().collect(Collectors.toList());
System.out.println(employeeIds);
}
When you run the above program, you will get the below output.
[400, 300, 200, 101, 100]
Using Stream API over Map's EntrySet
Let's say you want to write a program to to collect Employee objects whose IDs are between 100 and 199,
then in this case you can use Stream API over Map's EntrySet
public static void convertMapEntrySetToList() {
Map<Integer, Employee> employeesMap = Map.of(100, new Employee("Employee A", "IT Support"),
101, new Employee("Employee B", "IT Support"),
200, new Employee("Employee C", "Logistics"),
300, new Employee("Employee D", "Administration"),
400, new Employee("Employee E", "Cyber Security")
);
List<Employee> employees = employeesMap.entrySet().stream().filter(entry->entry.getKey()>=100 && entry.getKey()<=199)
.map(Map.Entry::getValue).collect(Collectors.toList());
System.out.println(employees);
}
When you run the above program, you will get the below output.
[[name : Employee A, department : IT Support], [name : Employee B, department : IT Support]]
Above example complete source code can be found here on
Similar Articles
-
Convert a List of objects into a Map using Java Stream API (from Java 8 onwards) -
Filter and find an element from a List using Java Stream API (from Java 8 onwards) -
How to remove elements from a List -
How to remove elements from a List while iterating -
How to loop through elements of List in different ways