Core Java

Java 8 Stream – count() Example

Hello. In this tutorial, we will explain the count() method introduced in java 8.

1. Introduction

Before diving deep into the practice stuff let us understand the count() method introduced in java8 programming.

java 8 count
  • Stream.count() – Returns the number of elements in the stream with or without filter condition being applied
  • This is a terminal operation and once consumed the same stream cannot be used any further

2. Practice

Let us dive into some practice stuff from here and I am assuming that you already have the Java 1.8 or greater installed in your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Find count

Create a java file in the com.java8 package and add the following code to it.

Demo1.java

package com.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

// java code for Stream.count() to count the elements in the stream
public class Demo1 {

	private static void count(final List<String> names) {
		// using count() to count the number of elements and 
		// saving the result in a variable
		final long count = names.stream().count();
		System.out.println("Count without filter: " + count);
	}
	
	private static void countEmptyStrings(final List<String> names) {
		final long count = names.stream()
				.filter(String::isEmpty)
				.count();
		System.out.println("\nEmpty element(s) count: " + count);
	}
	
	private static void countWithFilters(final List<String> names) {
		final Predicate<String> nameStartsWithA = ele -> ele.startsWith("A");
		final Predicate<String> nameStartsWithJ = ele -> ele.startsWith("J");
		
		final long count = names.stream()
				.filter(nameStartsWithA.or(nameStartsWithJ))
				.count();
		System.out.println("\nCount with filters: " + count);
	}
	
	public static void main(String[] args) {
		// creating a list of names
		final List<String> names = new ArrayList<>();
		names.add("John");
		names.add("Jane");
		names.add("Adam");
		names.add("Mathew");
		names.add("Eve");
		names.add("Kierra");
		names.add("Miranda");
		names.add("Thomas");
		names.add("Alvaro");
		names.add("");
		
		System.out.println("----- count() in Java8 -----\n");
		count(names);
		countEmptyStrings(names);
		countWithFilters(names);
	}
}

Run the file and if everything goes well the following output will be logged in the IDE console.

Console output

----- count() in Java8 -----

Count without filter: 10

Empty element(s) count: 1

Count with filters: 4

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned the count() method introduced in java8 programming along with the implementation. The count() method helps to determine the count of elements present in a stream. You can download the source code from the Downloads section.

4. Download the Eclipse Project

This was a tutorial on learning and implementing the count() method in java8 programming.

Download
You can download the full source code of this example here: Java 8 Stream – count() Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button