Home > Net >  How to count a nested collection in a List with Java Streams
How to count a nested collection in a List with Java Streams

Time:04-13

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!

CodePudding user response:

I think you are looking for something like

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