Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter inside Filter

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading