Print number of words in sheet stream api java 8


There are 2 string sheets: List<String> fileList and List<String> arr . It is required to display how many words in fileList are contained in arr, I did this:

    .filter(s -> arr

Is this approach correct? Or is there some better way?


It will be faster and easier to use the intersection of sets:

Set<String> set = new HashSet<>(fileList);
set.retainAll(new HashSet<String>(arr));

int count = set.size();

And if you already use streams, then do not produce them for each element. It will be more efficient like this:

long count =
Scroll to Top