Real World Example

Flipkart-style Sales Report

Analytics dashboards answer questions like "top selling categories this week", "revenue per city" and "best customer". Streams turn each question into a short pipeline.

Example

A daily sales report built from a list of order lines.

import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Sale> sales = List.of(
                new Sale("C1", "Bengaluru", "Mobiles", 2, 12999), new Sale("C2", "Delhi", "Fashion", 3, 799),
                new Sale("C1", "Bengaluru", "Books", 4, 350), new Sale("C3", "Chennai", "Mobiles", 1, 24999),
                new Sale("C4", "Delhi", "Mobiles", 1, 8999), new Sale("C2", "Delhi", "Books", 2, 499),
                new Sale("C5", "Chennai", "Fashion", 5, 999));

        System.out.println("Total revenue: Rs " + sales.stream().mapToInt(Sale::total).sum());

        System.out.println("Top 2 categories:");
        sales.stream()
                .collect(Collectors.groupingBy(Sale::category, Collectors.summingInt(Sale::total)))
                .entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .limit(2)
                .forEach(e -> System.out.println("  " + e.getKey() + " -> " + e.getValue()));

        Map<String, Integer> byCity = sales.stream()
                .collect(Collectors.groupingBy(Sale::city, TreeMap::new, Collectors.summingInt(Sale::total)));
        System.out.println("Revenue by city: " + byCity);

        String bestCustomer = sales.stream()
                .collect(Collectors.groupingBy(Sale::customer, Collectors.summingInt(Sale::total)))
                .entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse("none");
        System.out.println("Best customer: " + bestCustomer);

        System.out.println("Bulk buyers (qty >= 4): " + sales.stream().filter(s -> s.qty() >= 4)
                .map(Sale::customer).distinct().sorted().toList());
    }
}

record Sale(String customer, String city, String category, int qty, int price) {
    int total() { return qty * price; }
}

Output

Total revenue: Rs 69786
Top 2 categories:
  Mobiles -> 59996
  Fashion -> 7392
Revenue by city: {Bengaluru=27398, Chennai=29994, Delhi=12394}
Best customer: C1
Bulk buyers (qty >= 4): [C1, C5]

Where Else

  • Spring Data: transforming entities into DTOs: repo.findAll().stream().map(ProductDto::from).toList().
  • Banking: monthly statement totals by category (food, travel, bills) with groupingBy.
  • Log analysis: Files.lines(path).filter(l -> l.contains("ERROR")) grouped by service.