The method Collectors#counting collects the counts of the stream elements.
package com.logicbig.example.collectors;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CountingExample {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
Long c = s.collect(Collectors.counting());
System.out.println(c);
}
}
Output
3
Original Post