Java Stream count() with examples

 231 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream count() with examples.

count() is a part of the Stream pipeline and terminal operation. It belongs to the Stream interface.

count() operation allows us to count the number of elements in the stream. Once count() is invoked on a stream, it returns a count value and the stream will be closed. We cannot reuse the stream as we have invoked a terminal operation i.e., count().

Method Description

long count() - Returns the count of elements in this stream

It is also equivalent to return mapToLong(e -> 1L).sum();

Let us now see a few examples of Java 8 stream count()

Java stream count() operation example

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

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);

        long count = list.stream().count();
        
        System.out.println("Count is : " +  count);
       
    }
}

Output

Count is : 8

Java stream count() operation with filter example

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

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);

        long count = list.stream().filter(i -> i >= 3).count();

        System.out.println("Count is : " + count);

    }
}

Output

Count is : 6

Java stream count() operation reuse stream example

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

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);

        Stream<Integer> stream = list.stream();
        long count = stream.count();

        System.out.println("Count is : " + count);
        //Reuse the stream
        count = stream.filter(i -> i >= 3).count();

        System.out.println("Count is : " + count);

    }
}

Output

Count is : 8
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
	at java.util.stream.AbstractPipeline.<init>(Unknown Source)
	at java.util.stream.ReferencePipeline.<init>(Unknown Source)
	at java.util.stream.ReferencePipeline$StatelessOp.<init>(Unknown Source)
	at java.util.stream.ReferencePipeline$2.<init>(Unknown Source)
	at java.util.stream.ReferencePipeline.filter(Unknown Source)
	at StaticReference.main(StaticReference.java:25)

Java stream mapToLong() example instead of count()

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

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);

        long count = list.stream().mapToLong(i -> 1L).sum();
        System.out.println("Count is : " + count);

    }
}

Output

Count is : 8

Conclusion

count() operation is a terminal operation and it is used for counting the number of elements in the stream.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

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 (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.