In this article we are going to look at different ways of converting a Map into a List of objects in Java.
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)
- Using Stream API over Map's values
- Using Stream API over Map's KeySet
- Using Stream API over Map's EntrySet
In order to follow below examples, create a class called Employee and declare two variables,
name as String and department as String.
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+"]";
}
}
Let's write a program to create a List<String> with employee names from the input Map<Integer, Employee>
where key is the employee id and the value is Employee object.
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() method on Stream, then map them to List.
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]
Let's write a program to create a List<Integer> with employee IDs from the input Map<Integer, Employee>
where key is the employee id and the value is Employee object.
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]
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 Github
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