This post is about how to deal with when we get the Type mismatch: cannot convert from List<List<Object>> to List<Object> kind of error in Java 8.
For illustrating this, I have taken the Employees with matching technical skills example.
SkillsList.java
package com.digitizedpost.skills;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class SkillsList {
private List<Skills> getSkillsList() {
List<Skills> skillsList = new ArrayList<>();
skillsList.add(new Skills(1, "Java"));
skillsList.add(new Skills(2, ".NET"));
skillsList.add(new Skills(3, "Angular"));
skillsList.add(new Skills(4, "C"));
skillsList.add(new Skills(5, "C++"));
skillsList.add(new Skills(6, "JavaScript"));
return skillsList;
}
private List<Employee> getEmployeesWithMatchingSkills(List<Skills> skills) {
List<Employee> matchingSkills = skills.stream().
map(skill -> getEmployeeSkills(skill.getSkillId())).
flatMap(Collection::stream).
collect(Collectors.toList());
return matchingSkills;
}
private List<Employee> getEmployeeSkills(Integer skillId) {
List<Employee> employeeSkills = Arrays.asList(
new Employee(100, 1, "Andrew"),
new Employee(101, 2, "Morgan"),
new Employee(102, 1, "Sam"),
new Employee(103, 4, "Richardson"),
new Employee(104, 1, "Jack"),
new Employee(105, 6, "Simon"));
for(Employee employee : employeeSkills) {
if(employee.getSkillId().equals(skillId)) {
System.out.println(skillId);
employeeSkills = employeeSkills.stream()
.filter(emp -> emp.getSkillId().equals(skillId)).
collect(Collectors.toList()) ;
return employeeSkills;
}
}
return new ArrayList<Employee>();
}
public static void main(String[] args) {
List<Skills> skills = new SkillsList().getSkillsList();
List<Employee> employees = new SkillsList().getEmployeesWithMatchingSkills(skills);
for(Employee employee : employees) {
System.out.println("Skill Id:"+employee.getSkillId()+", "
+ "Employee Name:"+employee.getEmployeeName());
}
}
}
getSkillsList() method returns the technical skills. getEmployeeSkills() returns employee with specified technical skills. If employeeSkills is empty we will return an empty ArrayList.
Skills.java
package com.digitizedpost.skills;
public class Skills {
private Integer skillId;
private String skillName;
public Skills(Integer skillId, String skillName) {
this.skillId = skillId;
this.skillName = skillName;
}
//getters/setters
}
Employee.java
package com.digitizedpost.skills;
public class Employee {
private Integer employeeId;
private Integer skillId;
private String employeeName;
public Employee(Integer employeeId, Integer skillId, String employeeName) {
this.employeeId = employeeId;
this.skillId = skillId;
this.employeeName = employeeName;
}
//getters/setters
}
From the above example when flatmap is not used it will throw the below error,
Type mismatch: cannot convert from List<List<Employee>> to List<Employee>
Without flatmap() the code will look like,
private List<List<Employee>> getEmployeesWithMatchingSkills(List<Skills> skills) {
List<List<Employee>> matchingSkills = skills.stream().
map(skill -> getEmployeeSkills(skill.getSkillId())).
collect(Collectors.toList());
return matchingSkills;
}
getEmployeeSkills() method returns a List of employees object. If the resulting output of map is collected to list then it will collect list of list of employees. For this purpose we are adding flatmap() after the map() operation.
Skills id is mapped to employee skillId. Skills id is unique and employee skillId will not be unique and will repeat for different employees. So that we can find employees with same skills.
When you run the above example you can see the below output,
1
2
4
6
Skill Id:1, Employee Name:Andrew
Skill Id:1, Employee Name:Sam
Skill Id:1, Employee Name:Jack
Skill Id:2, Employee Name:Morgan
Skill Id:4, Employee Name:Richardson
Skill Id:6, Employee Name:Simon
The employee skills are ordered according to skill id as we are filtering the result according to skill id and then collecting it to list.
This is the best example for flatmap() where it is used to resolve the cannot convert from nested list to list error.