Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Stream.count() vs Collectors.counting()

Are there any differences between the count() method in the Stream interface vs counting() in Collectors? Which one should one use in general? Are there any performance benefits of using one over the other?

like image 524
Hiro_Hamada Avatar asked Dec 01 '22 08:12

Hiro_Hamada


2 Answers

One difference is with the implementation. This is documented on Stream.count() (at least on versions 9+):

An implementation may choose to not execute the stream pipeline (either sequentially or in parallel) if it is capable of computing the count directly from the stream source. In such cases no source elements will be traversed and no intermediate operations will be evaluated...

Try this:

System.out.println(Stream.of(1, 2, 3, 4, 5)
        .map(i -> {
            System.out.println("processing " + i);
            return i * 2;
        }).count());

System.out.println(Stream.of(1, 2, 3, 4, 5)
        .map(i -> {
            System.out.println("processing " + i);
            return i * 2;
        }).collect(Collectors.counting()));

Both will give you the correct count, but the first one may skip the execution of map(), thus might not show println's output. As documented, this is implementation detail (count() may skip intermediate operations if it can determine the number of elements without executing the pipeline)

like image 159
ernest_k Avatar answered Dec 04 '22 01:12

ernest_k


If you want to count (all) stream elements use count()

Returns the count of elements in this stream. This is a special case of a reduction and is equivalent to:

return mapToLong(e -> 1L).sum();

Use counting() when you need the count to be grouped, as:

Map<String, Long> collect = 
   wordsList.stream().collect(groupingBy(Function.identity(), counting())); 
like image 42
user7294900 Avatar answered Dec 03 '22 23:12

user7294900