Skip to main content
deleted 28 characters in body
Source Link
Pankaj
  • 3.7k
  • 17
  • 53
  • 89

I have collection of company and each company has list of department and department which are filtered based on complex multilevel condition. I would like to get the department info from some other source when there is no department found in a company and then proceed with filter condition as next step. Is the below implementation best way to achieve ?

 public class Company{ private List<Department> departments; } 
List<Company> newCompanies = companies.stream().forEach(c -> { if(CollectionUtils.isEmpty(c.getDepartments())){ //handle no department  //Set the department after getting from different source } });   newCompaniescompanies.stream() .filter(c -> CollectionUtils.isNotEmpty(c.getDepartments())) .filter(c -> c.getDepartments().stream() .anyMatch(d -> condition)) .collect(Collectors.toList()); 

I have collection of company and each company has list of department and department which are filtered based on complex multilevel condition. I would like to get the department info from some other source when there is no department found in a company and then proceed with filter condition as next step. Is the below implementation best way to achieve ?

 public class Company{ private List<Department> departments; } 
List<Company> newCompanies = companies.stream().forEach(c -> { if(CollectionUtils.isEmpty(c.getDepartments())){ //handle no department } }); newCompanies.stream() .filter(c -> CollectionUtils.isNotEmpty(c.getDepartments())) .filter(c -> c.getDepartments().stream() .anyMatch(d -> condition)) .collect(Collectors.toList()); 

I have collection of company and each company has list of department and department which are filtered based on complex multilevel condition. I would like to get the department info from some other source when there is no department found in a company and then proceed with filter condition as next step. Is the below implementation best way to achieve ?

 public class Company{ private List<Department> departments; } 
 companies.stream().forEach(c -> { if(CollectionUtils.isEmpty(c.getDepartments())){ //handle no department  //Set the department after getting from different source } });   companies.stream() .filter(c -> CollectionUtils.isNotEmpty(c.getDepartments())) .filter(c -> c.getDepartments().stream() .anyMatch(d -> condition)) .collect(Collectors.toList()); 
Source Link
Pankaj
  • 3.7k
  • 17
  • 53
  • 89

Handle null or empty collection with Java 8 streams

I have collection of company and each company has list of department and department which are filtered based on complex multilevel condition. I would like to get the department info from some other source when there is no department found in a company and then proceed with filter condition as next step. Is the below implementation best way to achieve ?

 public class Company{ private List<Department> departments; } 
List<Company> newCompanies = companies.stream().forEach(c -> { if(CollectionUtils.isEmpty(c.getDepartments())){ //handle no department } }); newCompanies.stream() .filter(c -> CollectionUtils.isNotEmpty(c.getDepartments())) .filter(c -> c.getDepartments().stream() .anyMatch(d -> condition)) .collect(Collectors.toList());