java
32 lines · 5 steps
Aggregating stats with IntSummaryStatistics
How a single stream pass collects count, min, max, sum, and average from a list of orders.
Explained by
highlit
1import java.util.List;
2import java.util.IntSummaryStatistics;
3
4public class OrderReport {
5
6 public record Order(String customer, int itemCount, double total) {}
7
8 public String summarizeItemCounts(List<Order> orders) {
9 IntSummaryStatistics stats = orders.stream()
10 .mapToInt(Order::itemCount)
11 .summaryStatistics();
12
13 if (stats.getCount() == 0) {
14 return "No orders to report.";
15 }
16
17 return String.format(
18 "Orders: %d | items min=%d, max=%d, sum=%d, avg=%.2f",
19 stats.getCount(),
20 stats.getMin(),
21 stats.getMax(),
22 stats.getSum(),
23 stats.getAverage());
24 }
25
26 public boolean hasBulkOrder(List<Order> orders, int threshold) {
27 return orders.stream()
28 .mapToInt(Order::itemCount)
29 .summaryStatistics()
30 .getMax() >= threshold;
31 }
32}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1IntSummaryStatistics gathers count, min, max, sum, and average in one stream traversal.
- 2Guarding against an empty stream avoids meaningless zero or infinity results before formatting.
- 3Method references like Order::itemCount turn a record accessor into a clean projection for mapToInt.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/aggregating-stats-with-intsummarystatistics-explained-java-5cc0/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.