Need to get the number of results that your Java Stream API operations returned?

In other words, you don't need the results themselves, but just the count.

Well, fortunately, you can get that info very easily. And in this guide I'll show you how to do it.

The Count

You can get the count with a method intuitively named count().

Let's look at an example here. First, let me establish the use case.

You want to get nothing more than the number of employees that match a specific criteria. You don't need to know their names or anything else. Just the number that matched.

Here's the model class for an employee:

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;

//getters and setters

}

Nice and neat. Just three properties. 

And here's some test data that you can use when testing out how to count results with the Java Stream API:

List<Employee> employees = new ArrayList<>();

Employee employee1 = new Employee();
employee1.setId("A17");
employee1.setFirstName("Frank");
employee1.setLastName("Smith");
employees.add(employee1);

Employee employee2 = new Employee();
employee2.setId("B12");
employee2.setFirstName("Bunny");
employee2.setLastName("Cheshire");
employees.add(employee2);

Employee employee3 = new Employee();
employee3.setId("C11");
employee3.setFirstName("Glenn");
employee3.setLastName("Kin");
employees.add(employee3);

There you go. A List object with just three (3) employees.

It's okay. You run a small business.

Now let's do something with that List.

The Whole Shebang

First, just count everything with no filter whatsoever. You can do that as follows:

Long count = employees
                .stream()
                .count();

System.err.println(count);

Note that the code above creates the Java Stream object with the stream() method.

Then it uses the count() method to return the number of elements in the Stream object. That number, by the way, gets returned as a Long.

Run that code above and it's going to give you a big fat 3 in red in console output because there are three Employee objects in that List.

Now let's try it with filtering.

With Filtering

If you're unsure about filtering with the Java Stream API, feel free to read my guide on that subject.

Try this code:

Long count = employees
                .stream()
                .filter(employee -> employee.getLastName().startsWith("K"))
                .count();

That filter() in the middle of the method chain returns only employees whose last names begin with "K".

Go through the test code above and you'll see that there's only one (1) employee whose last name begins with "K". So if you run that code above you'll get a count of just 1.

As a Long, of course.

You can add a count() after any filter() operation.

The Terminator

That count() method, by the way, is a terminal operation.

In the parlance of the Java Stream API, a terminal operation means "once you do this, you're done messing around with the Stream object and you get a return value."

So you can't do anything after count(). You can't do another filter. You can't do a map() operation. Nothing.

You could, of course, create a new Stream object and do some operations on that. But you're done with the current Stream object once you invoke a terminal operation.

That's something to keep in mind as you use count().

Wrapping It Up

Now you know how to count the results from a Java Stream operation.

Over to you. Take what you've learned here and make it your own.

Have fun!

Photo by Yan Krukov from Pexels