Home > Blockchain >  Filter inside Filter
Filter inside Filter

Time:12-02

I have a list of Object A. This Object A has a list of Object B as a property. Given that, I'm trying to filter an array of type A based in a property of the object of type B (let's say its an ID).

Example:

listOfObjectA = listOfObjectA.stream().filter(a -> a.getListOfB().stream().filter(b -> b.getId() == 10));

I have removed the .collect(Collectors.toList()) to make the post "cleaner".

With that, I'm getting this error: Type mismatch: cannot convert from List<B> to boolean.

I also tried with findAny, but had no good results either.

CodePudding user response:

Replace the second "filter" with "anyMatch". This would return a boolean value that will be used in the first "filter":

listOfObjectA = listOfObjectA.stream().filter(a -> a.getListOfB().stream().anyMatch(b -> b.getId() == 10));
  • Related