Stream.count()

The streams API provides a count method that returns the number of elements in the stream after filtering has been applied. Let's take the previous example to get a count of Products from the MADISON brand:

long count = searchResult.stream()        .filter(item -> item.getName().startsWith("MADISON"))        .count();System.out.println("The number of products from MADISON are: " + count);

The count() method returns a long, which is the count of elements matching with the filter criteria. In this example, the following output will be displayed on the console:

The number of products from MADISON are: 2

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.