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());
forEach
returns void. Are you trying to return a list of new companies that are modified versions of the existing ones? Or do you want to mutate the existing companies? Is the listnewCompanies
going to contain the same number of companies as the original listcompanies
?map(department -> if(...) return from other source; else return departments).filter(...)