Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this tutorial, we’ll explore the use of the Stream.count() method. Specifically, we'll see how we can combine the count() method with the filter() method to count the matches of a Predicate we've applied.

2. Using Stream.count()

The count() method itself provides a small but very useful functionality. We can also combine it excellently with other tools, for example with Stream.filter().

Let's use the same Customer class that we defined in our tutorial for Stream.filter():

public class Customer {
    private String name;
    private int points;
    //Constructor and standard getters

In addition, we also create the same collection of customers:

Customer john = new Customer("John P.", 15);
Customer sarah = new Customer("Sarah M.", 200);
Customer charles = new Customer("Charles B.", 150);
Customer mary = new Customer("Mary T.", 1);

List<Customer> customers = Arrays.asList(john, sarah, charles, mary);

Next, we'll apply Stream methods on the list to filter it and determine how many matches our filters get.

2.1. Counting Elements

Let's see the very basic usage of count():

long count = customers.stream().count();


Note that count() returns a long value.

2.2. Using count() With filter()

The example in the previous subsection wasn't really impressive. We could have come to the same result with the List.size() method.

Stream.count() really shines when we combine it with other Stream methods – most often with filter():

long countBigCustomers = customers
  .filter(c -> c.getPoints() > 100)


In this example, we've applied a filter on the list of customers, and we've also obtained the number of customers that fulfill the condition. In this case, we have two customers with more than 100 points.

Of course, it can also happen that no element matches our filter:

long count = customers
  .filter(c -> c.getPoints() > 500)


2.3. Using count() With Advanced Filters

In our tutorial about filter(), we saw some more advanced use cases of the method. Of course, we can still count the result of such filter() operations.

We can filter collections with multiple criteria:

long count = customers
  .filter(c -> c.getPoints() > 10 && c.getName().startsWith("Charles"))


Here, we filtered and counted the number of customers whose names start with “Charles” and who have more than 10 points.

We can also extract the criteria into its own method and use method reference:

long count = customers


3. Conclusion

In this article, we saw some examples of how to use the count() method in combination with the filter() method to process streams. For further use cases of count(), check out other methods that return a Stream, such as those shown in our tutorial about merging streams with concat().

As always, the complete code is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!