Course Content
Stream API
Stream API
Retrieving Stream Summary Metrics with summaryStatistics() Method
When processing data, it's often necessary to calculate statistical metrics such as sum, average, minimum, maximum, and element count.
You've already learned how to find the minimum and maximum values in a stream, but there's a method that gathers all these statistics at once. The Java Stream API provides a dedicated method called summaryStatistics()
, which simplifies retrieving overall statistics from numeric streams.
Method summaryStatistics()
Stream API also provides specialized numeric streams: IntStream
, LongStream
, and DoubleStream
. These work just like regular streams but are optimized for specific primitive types.
Each of these numeric streams includes a summaryStatistics()
method, which returns an instance of IntSummaryStatistics
, LongSummaryStatistics
, or DoubleSummaryStatistics
, respectively. These objects contain key statistical metrics, such as count, sum, min, max, and average.
By calling summaryStatistics()
on a numeric stream, you can quickly gather and analyze statistical data about its values.
Key Methods
In Java, the IntSummaryStatistics
, LongSummaryStatistics
, and DoubleSummaryStatistics
classes are used to collect statistics on numeric data. They provide a set of methods for extracting key statistical metrics.
Practical Example
In an online store, you need to analyze product prices to determine the minimum and maximum prices, the average price, and the total number of items.
Main
package com.example; import java.util.List; import java.util.IntSummaryStatistics; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Integer> prices = List.of(1500, 2000, 3500, 1200, 4000, 2500); IntSummaryStatistics stats = prices.stream() .collect(Collectors.summarizingInt(Integer::intValue)); System.out.println("Minimum price: " + stats.getMin()); System.out.println("Maximum price: " + stats.getMax()); System.out.println("Average price: " + stats.getAverage()); System.out.println("Total number of items: " + stats.getCount()); System.out.println("Total sum: " + stats.getSum()); } }
In this code, you create a list of product prices and convert it into a numeric stream. The Collectors.summarizingInt(Integer::intValue)
method gathers statistical data and stores it in an IntSummaryStatistics
object. You then use the methods getMin()
, getMax()
, getAverage()
, getCount()
, and getSum()
to extract the necessary values.
Additional Methods
The combine()
method merges two statistics objects. This is useful when processing data in multiple threads or when consolidating statistics from different sources.
The accept()
method manually adds values to a statistics object, which is helpful for dynamically updating data.
Main
package com.example; import java.util.IntSummaryStatistics; public class Main { public static void main(String[] args) { IntSummaryStatistics stats1 = new IntSummaryStatistics(); stats1.accept(1500); stats1.accept(2500); IntSummaryStatistics stats2 = new IntSummaryStatistics(); stats2.accept(3500); stats2.accept(4500); stats1.combine(stats2); System.out.println("Minimum price: " + stats1.getMin()); System.out.println("Maximum price: " + stats1.getMax()); System.out.println("Average price: " + stats1.getAverage()); System.out.println("Total number of items: " + stats1.getCount()); System.out.println("Total sum: " + stats1.getSum()); } }
In this example, you create two IntSummaryStatistics
objects, each containing values added using the accept()
method. The combine()
method then merges the statistics from both objects, updating the total count, sum, and min/max values.
As a result, stats1
now holds the combined statistics for all numbers, which are then printed to the console.
1. Which class is used to collect statistics for int
values in the Stream API?
2. Which method adds a value to an IntSummaryStatistics
object?
3. Which method merges two IntSummaryStatistics
objects?
Thanks for your feedback!