How to use Collectors.counting() in Java 8

 202 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about how to use Collectors.counting() in Java 8.

If you would like to learn about Java 8 Stream operations.Please check out this link.

Collectors.counting() in Java 8 allows us to count the number of elements in the stream and provide the output as Long.

Collectors.counting() Syntax

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.

Collectors.counting() example


import java.util.Arrays;
import java.util.List;

public class StaticReference
    public static void main(String[] args)

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", "banana", "orange", "apple");

        Long count =;






References : Collectors.counting()

I hope you like this tutorial. Thanks for reading and Please leave us a comment below!

Tags :

About the Author


Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us ( and give a thumbs up :) Feel free to contact me for any queries!.