'How to count a nested collection in a List with Java Streams

I need to count the elements in the next list I have a listArtif list and for each of their elements I have a list of findings and for each of the findings objects I have a field called isChecked, so I need to count all the checked findings in the listArtif list. I've done the next but I'm getting the total of elements in the listArtif list.

artifactList.stream().map(artifact -> artList.getFindings().stream().filter(finding -> finding.isChecked()).count());

Any ideas?

Thanks!



Solution 1:[1]

I think you are looking for something like

artifactList.stream()
            .flatMap(artifact -> artifact.getFindings().stream())
            .filter(finding -> finding.isChecked())
            .count();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eritrean