I have a parent class Company
which has a list of Employee
objects. I need to create a stream of Parent
class who has an Employee
with the phone number mentioned.
Company cmp1 = new Company();
cmp1.setName("oracle");
Company cmp2 = new Company();
cmp1.setName("Google");
Employee emp1 = new Employee();
emp1.setName("David");
emp1.setPhone("900");
Employee emp2 = new Employee();
emp2.setName("George");
emp2.setPhone("800");
Employee emp4 = new Employee();
emp4.setName("BOB");
emp4.setPhone("300");
Employee emp5 = new Employee();
emp5.setName("taylor");
emp5.setPhone("900");
List<Employee> cmp1EmpList1 = new ArrayList<Employee>();
cmp1EmpList1.add(emp1);
cmp1EmpList1.add(emp2);
cmp1.setEmployees(cmp1EmpList1);
List<Employee> cmp1EmpList2 = new ArrayList<Employee>();
cmp1EmpList2.add(emp4);
cmp1EmpList2.add(emp5);
cmp2.setEmployees(cmp1EmpList2);
List<Company> companies = Arrays.asList(cmp1, cmp2);
To retrieve the stream I have tried adding the below code
List<Company> companiesWithEmployeesphone800 = companies.stream()
.filter(loc -> loc.getEmployees()
.stream()
.flatMap(locnew -> locnew.getPhone()
.equalsIgnoreCase("800"))
)
.collect(Collectors.toList());
but received incompatible types.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…