Filter a stream of a class using a value of subclass

前端 未结 1 906
生来不讨喜
生来不讨喜 2021-01-24 06:15

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

相关标签:
1条回答
  • 2021-01-24 06:36

    Use anyMatch for a predicate instead of flatMap as:

    List<Company> companiesWithEmployeesphone800 =
        companies.stream()
                .filter(loc -> loc.getEmployees().stream()
                        .anyMatch(locnew -> locnew.getPhone().equalsIgnoreCase("800")))
                .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题