Collectors counting() method is used to count the number of elements passed in the stream as the parameter. It returns a Collector accepting elements of type T that counts the number of input elements. If no elements are present, the result is 0. It is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. It returns the total count of elements in the stream which reach the collect() method after undergoing various pipelined stream operations such as filtering, reduction etc.
Syntax:
public static <T> Collector<T, ?, Long> counting()
where the mentioned terms are as follows:
- Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
- T: The type of input elements to the reduction operation.
- A: The mutable accumulation type of the reduction operation.
- R: The result type of the reduction operation.
- Long: The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long. In addition, this class provides several methods for converting a long to a String and a String to a long, as well as other constants and methods useful when dealing with a long.
- T: The type of the input elements.
Parameters: This method does not take any parameter.
Return Value: A Collector that counts the input elements. The count is returned as Long object.
Below are examples to illustrate counting() method:
Program 1:
// Java code to show the implementation of // Collectors counting() method import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a stream of strings Stream<String> s = Stream.of("1", "2", "3", "4"); // using Collectors counting() method to // count the number of input elements long ans = s.collect(Collectors.counting()); // displaying the required count System.out.println(ans); } } |
4
Program 2: When no element is passed as input element.
// Java code to show the implementation of // Collectors counting() method import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a stream of strings Stream<String> s = Stream.of(); // using Collectors counting() method to // count the number of input elements long ans = s.collect(Collectors.counting()); // displaying the required count System.out.println(ans); } } |
0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java 8 | Collectors averagingInt() with Examples
- Java | Collectors averagingDouble() with Examples
- Collectors toSet() in Java with Examples
- Collectors toList() method in Java with Examples
- Collectors toMap() method in Java with Examples
- Collectors collectingAndThen() method in Java with Examples
- Collectors groupingBy() method in Java with Examples
- Java 8 Streams | Collectors.joining() method with Examples
- Java | Collectors averagingLong (ToLongFunction mapper) with Examples
- Java streams counting() method with examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- Java Collectors
- Collectors partitioningBy() method in Java
- Java Stream | Collectors toCollection() in Java
- Types of JVM Garbage Collectors in Java with implementation details
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.IntPredicate interface in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

