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