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
package com.example.shop.repository; import com.example.shop.domain.Order; import com.example.shop.domain.OrderStatus;
Deriving JPA queries from method names in Spring
query derivation
repositories
pagination
Intermediate
8 steps
python
from django.core.cache import cache from django.db.models import Count, Sum, Q from django.utils import timezone
Caching a Django dashboard aggregate query
caching
aggregation
conditional-queries
Intermediate
7 steps
java
public class OrderRepository { private static final int BATCH_SIZE = 500; private static final String INSERT_SQL =
Batched JDBC inserts with transaction safety
jdbc
batching
transactions
Intermediate
8 steps
java
@RestControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
Centralized error handling in Spring
exception-handling
problemdetail
rest-api
Intermediate
7 steps
java
public List<String> collectTagsFromArticles(List<Article> articles) { return articles.stream() .flatMap(article -> article.getTags().stream()) .map(String::trim)
Flattening nested data with Java streams
streams
flatmap
collectors
Intermediate
7 steps
java
@Component @Order(20) public class ProductSearchIndexRunner implements ApplicationRunner {
Rebuilding a search index at Spring startup
startup-hook
batching
streaming
Intermediate
8 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.