I asked already a few months ago how to count the elements of a Stream. My new approach is:
int count = stream.mapToInt(element -> 1).sum();
What is the most elegant solution in your opinion?
I asked already a few months ago how to count the elements of a Stream. My new approach is:
int count = stream.mapToInt(element -> 1).sum();
What is the most elegant solution in your opinion?
From Oracle's Documentation on Stream:
Reduction operations
A reduction operation (also called a fold) takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation, such as finding the sum or maximum of a set of numbers, or accumulating elements into a list. The streams classes have multiple forms of general reduction operations, called reduce() and collect(), as well as multiple specialized reduction forms such as sum(), max(), or count().
You were using one of the reduction operations from stream, there just happens to be others as well