June 2018
Beginner
722 pages
18h 47m
English
The long count() Terminal operation of the Stream interface looks straightforward and benign. It returns the number of elements in this stream. Those who are used to working with collections and arrays may use the count() operation without thinking twice. Here is an example that proves it works just fine:
long count = Stream.of("1", "2", "3", "4", "5") .peek(System.out::print) .count();System.out.print(count); //prints: 5
As you can see, the code that implements the method count was able to determine the stream size without executing all the pipe. The values of elements were not printed by the peek() operation, which proves that the elements were not emitted. But it is not always possible to determine the stream size ...
Read now
Unlock full access