Java 8 Streams Java Java API
java.util.stream.Collectors
public static <T> Collector<T,?,Long> counting()
Returns a Collector accepting elements of type T that counts the number of input elements. If no elements are present, the result is 0.
Collector
T
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); }}
3