Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Calculating Stream Statistics with count(), max(), and min() | Terminal Operations in the Stream API
Stream API
course content

Course Content

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Calculating Stream Statistics with count(), max(), and min()

In programming, you often need to count the number of elements in a collection or find the minimum and maximum values among them.

However, programmers usually don't write custom algorithms for these tasks since built-in methods already exist. The Stream API provides convenient methods for working with collections, allowing computations on stream elements.

Among these methods are count(), max(), and min(), which help determine the number of elements and find the largest and smallest values.

The count() Method

The count() method returns the number of elements in a stream. It's often used when you need to determine the size of a collection after filtering data.

It takes no parameters and returns a primitive long type.

Practical Example

In an online store, you need to count the number of products with a discount of more than 20%.

java

Main

copy
123456789101112131415161718192021222324252627282930313233
package com.example; import java.util.List; public class Main { public static void main(String[] args) { List<Product> products = List.of( new Product("Laptop", 15.0), new Product("Smartphone", 25.0), new Product("Tablet", 30.0) ); long discountedProducts = products.stream() .filter(product -> product.getDiscount() > 20.0) .count(); System.out.println("Number of products with more than 20% discount: " + discountedProducts); } } class Product { private String name; private double discount; public Product(String name, double discount) { this.name = name; this.discount = discount; } public double getDiscount() { return discount; } }

In this example, the filter() method selects products with a discount greater than 20%, and count() determines their quantity.

The max() Method

The max() method returns the maximum element in a stream based on a given Comparator. If the stream is empty, it returns Optional.empty().

Practical Example

Find the most expensive product in an online store to promote it as a premium product.

java

Main

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package com.example; import java.util.List; import java.util.Optional; import java.util.Comparator; public class Main { public static void main(String[] args) { List<Product> products = List.of( new Product("Laptop", 1200.0), new Product("Smartphone", 800.0), new Product("Tablet", 600.0) ); Optional<Product> mostExpensiveProduct = products.stream() .max(Comparator.comparing(Product::getPrice)); mostExpensiveProduct.ifPresent(product -> System.out.println("Most expensive product: " + product.getName() + " - $" + product.getPrice()) ); } } class Product { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public double getPrice() { return price; } public String getName() { return name; } }

In this code, Comparator.comparing(Product::getPrice) compares products by price, and max() selects the most expensive one.

The min() Method

The min() method is similar to max(), but it returns the smallest element in a stream based on a given Comparator.

Practical Example

Find the product with the lowest stock to assess the need for restocking.

java

Main

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package com.example; import java.util.List; import java.util.Optional; import java.util.Comparator; public class Main { public static void main(String[] args) { List<Product> products = List.of( new Product("Laptop", 5), new Product("Smartphone", 0), new Product("Tablet", 2) ); Optional<Product> leastStockProduct = products.stream() .min(Comparator.comparing(Product::getStock)); leastStockProduct.ifPresent(product -> System.out.println("Product with the lowest stock: " + product.getName() + " - " + product.getStock() + " units") ); } } class Product { private String name; private int stock; public Product(String name, int stock) { this.name = name; this.stock = stock; } public int getStock() { return stock; } public String getName() { return name; } }

This code identifies the product with the lowest stock using the min() method and Comparator.comparing(Product::getStock).

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 8
We're sorry to hear that something went wrong. What happened?
some-alt