I want to apply a second filter for my product category list as shown below:
final List<ProductCategoryDTO> productCategoryList = productCategoryService .findAllByUuid(uuid) .stream() .filter(category -> !category.getProductList().isEmpty()) // I am trying to filter the productCategoryList based on // the "isDisabled" value of inner list >>> .filter(category -> category.getProductList().stream() .anyMatch(p -> !p.getMenuProperties().isDisabled())) .collect(Collectors.toList()); The first filter !category.getProductList().isEmpty() is applied to the productCategoryList (outer list) and then I also want to apply a filter based on inner list's isDisabled value. I tried to use flatMap and concatenate the filter conditions as shown below, but none of them is not working:
.filter(category -> !category.getProductList().isEmpty() && !category.getProductList().stream() .anyMatch(p -> p.getMenuProperties().isDisabled())) So, how can I filter productCategoryList based on inner list value?
Here are the related DTO's:
public class ProductCategoryDTO { private UUID uuid; private List<MenuCategoryDTO> productList; } public class MenuCategoryDTO { private MenuPropertiesDTO menuProperties; } Update: I also need to retrieve the list of UUID values of products by flattening the lists as shown below. But it does not work:
final List<UUID> productUuidList = productCategoryList.stream() .flatMap(a -> a.getProductList().stream()) .flatMap(b -> b.getMenuProperties().getUuid()) .collect(Collectors.toList()); Any idea?
anyMatch( ! condition)IS NOT same as! anyMatch(condition). First one returns true if at least one is not disabled, whereas the second return true only when none are disabledanyMatch, but I already ask the true usage example. So, any suggestion please?noneMatchinstead of!anymatch->filter(category -> !category.getProductList().isEmpty() && category.getProductList().stream() .noneMatch(p -> p.getMenuProperties().isDisabled()))getMenuProperties().isDisabled()is true and false. I am not sure ifmatchmethods are used for boolean result. Any idea by using different approaches e.g.flatMap?