1

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?

7
  • Your code in the first filter is different from the second code. 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 disabled Commented Feb 22, 2022 at 13:11
  • Then I misused anyMatch, but I already ask the true usage example. So, any suggestion please? Commented Feb 22, 2022 at 13:12
  • Your second code snippet should work. But I suggest noneMatch instead of !anymatch -> filter(category -> !category.getProductList().isEmpty() && category.getProductList().stream() .noneMatch(p -> p.getMenuProperties().isDisabled())) Commented Feb 22, 2022 at 13:14
  • Thanks, but it returns 0 record even if there are mixed records with getMenuProperties().isDisabled() is true and false. I am not sure if match methods are used for boolean result. Any idea by using different approaches e.g. flatMap ? Commented Feb 22, 2022 at 13:20
  • Yes, AllMatch, AnyMatch and NoneMatch are not suitable for my situation, because I need a list instead of boolean result. Any idea? Commented Feb 22, 2022 at 13:23

2 Answers 2

1

If your category.getProductList() is not an immutable list, then you could use

List<ProductCategoryDTO> list = productCategoryService.findAllByUuid(uuid); list.stream() .filter(category -> !category.getProductList().isEmpty()) .forEach(category -> category.getProductList() .removeIf(p -> p.getMenuProperties().isDisabled())); 
Sign up to request clarification or add additional context in comments.

3 Comments

On the other hand, how can I get the UUID of the list by flattening products in each categories? Assume that there are 2 categories and in each category, there are 3 products. So, I need to retrieve 6 UUID of these products. How can I do that? I tried but failed :(
I added an update regarding to building uuid list. Any idea?
@Ashley The second one should be map instead of flatMap
0

You get Stream of MenuCategoryDTO object after 1st flatMap. So you don't need 2nd flatMap. You can use Map instead.

final List<UUID> productUuidList = productCategoryList.stream() .flatMap(a -> a.getProductList().stream()) .map(b -> b.getMenuProperties().getUuid()) .collect(Collectors.toList()); 

Comments