The java.util.stream.Collectors counting method returns a Collector that counts the number of elements in a stream. This method is used to count the occurrence of elements in a stream.
Example:
Suppose we have a list of integers, and we want to count the number of even integers in the list.
long count = numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.counting());
System.out.println(count);
Output: 5
In this example, we are using the Collectors counting method to count the number of even numbers in the list. The filter() method is used to filter out the even numbers. The counting() method then counts the number of filtered elements.
Package Library:
The Collectors counting method is a part of the Java SE Library and is included in the java.util.stream package.
Java Collectors.counting - 15 examples found. These are the top rated real world Java examples of java.util.stream.Collectors.counting extracted from open source projects. You can rate examples to help us improve the quality of examples.