Definition
- Stream is a sequence of elements that supports sequential and parallel aggregate operations; it does not store data and is consumed once.
- Intermediate operation is a lazy operation (filter, map, sorted, distinct) that returns a new Stream and executes nothing until a terminal operation runs.
- Terminal operation is the operation (collect, forEach, reduce, count) that triggers the pipeline to process elements and produce a result.
- Collector is an object that specifies how to accumulate stream elements into a result container; Collectors utility class provides toList, toMap, groupingBy, joining, and more.
- reduce() is a terminal operation that combines all elements into a single value using an identity, an accumulator, and optionally a combiner for parallel streams.
Think of the National Bank of India processing the day's transactions. The raw transaction log is the source. Rajesh sets up a conveyor belt (stream) with stations: one station filters only deposits (filter), the next station extracts the amount (map), and the final station sorts them by account (groupingBy). The conveyor belt does not start moving until Rajesh presses the button at the end (the terminal operation). Each station processes one transaction at a time as it passes through, and the belt stops when all transactions are processed.
Understanding the Concepts
What is a Java Stream?
A Stream is a pipeline for processing sequences of elements. You create a stream from a source (collection, array, generator), chain intermediate operations (filter, map, flatMap, sorted, distinct), and end with a terminal operation (collect, forEach, reduce, count) that produces a result.
Streams are lazy: intermediate operations do not process any elements until a terminal operation triggers the pipeline.
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
public class StreamBasics {
record Transaction(String accountId, String type, BigDecimal amount) {}
public static void main(String[] args) {
var transactions = List.of(
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("50000.00")),
new Transaction("CUR3001", "WITHDRAWAL", new BigDecimal("20000.00")),
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("75000.00")),
new Transaction("SAV2002", "DEPOSIT", new BigDecimal("10000.00")),
new Transaction("CUR3001", "DEPOSIT", new BigDecimal("300000.00"))
);
// Stream pipeline: filter deposits > 25000, extract amounts, sort
var highDeposits = transactions.stream()
.filter(t -> t.type().equals("DEPOSIT"))
.map(Transaction::amount)
.filter(a -> a.compareTo(new BigDecimal("25000")) > 0)
.sorted()
.toList();
System.out.println("High deposits (> 25000 INR): " + highDeposits);
System.out.println("Count: " + highDeposits.size());
}
}
Expected output:
High deposits (> 25000 INR): [50000.00, 75000.00, 300000.00]
Count: 3
How does the stream pipeline work (lazy evaluation)?
Intermediate operations are lazy: they build the pipeline but process nothing. When the terminal operation runs, elements flow through the pipeline one by one, not in separate passes. This is called "loop fusion": all operations are applied to each element in a single pass.
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
public class LazyEvaluationDemo {
record Transaction(String accountId, String type, BigDecimal amount) {}
public static void main(String[] args) {
var transactions = List.of(
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("50000")),
new Transaction("CUR3001", "WITHDRAWAL", new BigDecimal("20000")),
new Transaction("SAV2002", "DEPOSIT", new BigDecimal("10000")),
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("75000"))
);
System.out.println("Pipeline execution order (lazy, fused):");
var result = transactions.stream()
.peek(t -> System.out.println(" 1. Source: " + t.accountId() + " " + t.amount()))
.filter(t -> t.type().equals("DEPOSIT"))
.peek(t -> System.out.println(" 2. After filter: " + t.accountId()))
.map(Transaction::amount)
.peek(a -> System.out.println(" 3. After map: " + a))
.collect(Collectors.toList());
System.out.println("\nResult: " + result);
}
}
Expected output:
Pipeline execution order (lazy, fused):
1. Source: SAV2001 50000
2. After filter: SAV2001
3. After map: 50000
1. Source: CUR3001 20000
1. Source: SAV2002 10000
2. After filter: SAV2002
3. After map: 10000
1. Source: SAV2001 75000
2. After filter: SAV2001
3. After map: 75000
Result: [50000, 10000, 75000]
How does groupingBy work?
Collectors.groupingBy() groups stream elements by a classifier function into a Map where the key is the group and the value is a List of elements in that group. You can pass a downstream collector to change the value type (counting, summing, mapping, etc.).
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupingByDemo {
record Transaction(String accountId, String type, BigDecimal amount) {}
public static void main(String[] args) {
var transactions = List.of(
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("50000")),
new Transaction("SAV2001", "WITHDRAWAL", new BigDecimal("10000")),
new Transaction("CUR3001", "DEPOSIT", new BigDecimal("300000")),
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("75000")),
new Transaction("CUR3001", "WITHDRAWAL", new BigDecimal("50000")),
new Transaction("SAV2002", "DEPOSIT", new BigDecimal("25000"))
);
// Group by accountId
Map<String, List<Transaction>> byAccount = transactions.stream()
.collect(Collectors.groupingBy(Transaction::accountId));
System.out.println("Transactions grouped by account:");
byAccount.forEach((id, txns) -> {
System.out.println(" " + id + ": " + txns.size() + " transactions");
txns.forEach(t -> System.out.println(" " + t.type() + " " + t.amount() + " INR"));
});
// Group by type, count per group
Map<String, Long> countByType = transactions.stream()
.collect(Collectors.groupingBy(Transaction::type, Collectors.counting()));
System.out.println("\nCount by type: " + countByType);
// Group by accountId, sum amounts
Map<String, BigDecimal> sumByAccount = transactions.stream()
.collect(Collectors.groupingBy(
Transaction::accountId,
Collectors.reducing(BigDecimal.ZERO, Transaction::amount, BigDecimal::add)));
System.out.println("\nTotal by account:");
sumByAccount.forEach((id, total) -> System.out.println(" " + id + ": " + total + " INR"));
}
}
Expected output:
Transactions grouped by account:
SAV2001: 3 transactions
DEPOSIT 50000 INR
WITHDRAWAL 10000 INR
DEPOSIT 75000 INR
CUR3001: 2 transactions
DEPOSIT 300000 INR
WITHDRAWAL 50000 INR
SAV2002: 1 transactions
DEPOSIT 25000 INR
Count by type: {DEPOSIT=4, WITHDRAWAL=2}
Total by account:
SAV2001: 135000 INR
CUR3001: 350000 INR
SAV2002: 25000 INR
How does reduce work?
The reduce() operation combines all elements of a stream into a single result. It takes an identity (starting value) and an accumulator (function that combines two values). For parallel streams, it also takes a combiner.
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
public class ReduceDemo {
record Transaction(String accountId, String type, BigDecimal amount) {}
public static void main(String[] args) {
var transactions = List.of(
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("50000")),
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("75000")),
new Transaction("CUR3001", "DEPOSIT", new BigDecimal("300000")),
new Transaction("SAV2002", "DEPOSIT", new BigDecimal("25000"))
);
// Sum all deposit amounts
BigDecimal totalDeposits = transactions.stream()
.map(Transaction::amount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Total deposits: " + totalDeposits + " INR");
// Find maximum deposit
var maxDeposit = transactions.stream()
.map(Transaction::amount)
.reduce(BigDecimal::max);
System.out.println("Max deposit: " + maxDeposit.orElse(BigDecimal.ZERO) + " INR");
// Find minimum deposit
var minDeposit = transactions.stream()
.map(Transaction::amount)
.reduce(BigDecimal::min);
System.out.println("Min deposit: " + minDeposit.orElse(BigDecimal.ZERO) + " INR");
// Count using reduce
long count = transactions.stream()
.map(t -> 1L)
.reduce(0L, Long::sum);
System.out.println("Count: " + count);
}
}
Expected output:
Total deposits: 450000 INR
Max deposit: 300000 INR
Min deposit: 25000 INR
Count: 4
How is Stream different from Collection?
| Feature | Collection | Stream |
|---|---|---|
| Storage | Stores elements in memory | Does not store elements |
| Consumption | Can be iterated multiple times | Consumed once, then exhausted |
| Modification | Elements can be added/removed | Elements cannot be modified |
| Evaluation | Eager (all elements in memory) | Lazy (processed on demand) |
| Size | Known (size() method) | May be infinite |
| Ordering | Maintains insertion order (List) | Depends on source and operations |
| Parallelism | Manual (iterate and fork) | Built-in (.parallelStream()) |
| Use case | Data storage and retrieval | Data transformation and aggregation |
package com.bank.core;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class StreamVsCollection {
public static void main(String[] args) {
// Collection: stores data, reusable
List<BigDecimal> balances = new ArrayList<>();
balances.add(new BigDecimal("150000"));
balances.add(new BigDecimal("500000"));
balances.add(new BigDecimal("75000"));
System.out.println("Collection: iterate twice");
System.out.println(" First: " + balances.stream().count());
System.out.println(" Second: " + balances.stream().count());
// Stream: consumed once
Stream<BigDecimal> stream = balances.stream().filter(b -> b.compareTo(new BigDecimal("100000")) > 0);
System.out.println("\nStream: consumed once");
System.out.println(" Result: " + stream.toList());
try {
stream.count(); // reuse fails
} catch (IllegalStateException e) {
System.out.println(" Reuse: " + e.getMessage());
}
}
}
Expected output:
Collection: iterate twice
First: 3
Second: 3
Stream: consumed once
Result: [150000, 500000]
Reuse: stream has already been operated upon or closed
What is a complete banking project using streams?
This project processes a transaction list with streams: filter by amount, group by account, sum balances, collect to map, and reduce for totals.
Project structure
bank-stream-processor/ ├── src/ │ └── main/ │ └── java/ │ └── com/ │ └── bank/ │ └── core/ │ ├── Transaction.java │ ├── TransactionProcessor.java │ └── StreamBankApp.java └── run.sh
Transaction.java
package com.bank.core;
import java.math.BigDecimal;
public record Transaction(String txnId, String accountId, String type, BigDecimal amount) {
@Override
public String toString() {
return txnId + " [" + accountId + "] " + type + " " + amount + " INR";
}
}
TransactionProcessor.java
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TransactionProcessor {
private final List<Transaction> transactions;
public TransactionProcessor(List<Transaction> transactions) {
this.transactions = transactions;
}
public List<Transaction> filterByMinAmount(BigDecimal minAmount) {
return transactions.stream()
.filter(t -> t.amount().compareTo(minAmount) >= 0)
.toList();
}
public Map<String, List<Transaction>> groupByAccount() {
return transactions.stream()
.collect(Collectors.groupingBy(Transaction::accountId));
}
public Map<String, BigDecimal> sumByAccount() {
return transactions.stream()
.collect(Collectors.groupingBy(
Transaction::accountId,
Collectors.reducing(BigDecimal.ZERO, Transaction::amount, BigDecimal::add)));
}
public Map<String, Long> countByType() {
return transactions.stream()
.collect(Collectors.groupingBy(Transaction::type, Collectors.counting()));
}
public BigDecimal totalAmount() {
return transactions.stream()
.map(Transaction::amount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
}
StreamBankApp.java
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
public class StreamBankApp {
public static void main(String[] args) {
System.out.println("=== National Bank of India: Stream Processor ===");
var transactions = List.of(
new Transaction("T1", "SAV2001", "DEPOSIT", new BigDecimal("50000")),
new Transaction("T2", "SAV2001", "WITHDRAWAL", new BigDecimal("10000")),
new Transaction("T3", "CUR3001", "DEPOSIT", new BigDecimal("300000")),
new Transaction("T4", "SAV2001", "DEPOSIT", new BigDecimal("75000")),
new Transaction("T5", "CUR3001", "WITHDRAWAL", new BigDecimal("50000")),
new Transaction("T6", "SAV2002", "DEPOSIT", new BigDecimal("25000")),
new Transaction("T7", "SAV2002", "DEPOSIT", new BigDecimal("15000"))
);
var processor = new TransactionProcessor(transactions);
// Filter
System.out.println("\nTransactions >= 50000 INR:");
processor.filterByMinAmount(new BigDecimal("50000"))
.forEach(t -> System.out.println(" " + t));
// Group by account
System.out.println("\nGroup by account:");
processor.groupByAccount().forEach((id, txns) ->
System.out.println(" " + id + ": " + txns.size() + " transactions"));
// Sum by account
System.out.println("\nSum by account:");
processor.sumByAccount().forEach((id, total) ->
System.out.println(" " + id + ": " + total + " INR"));
// Count by type
System.out.println("\nCount by type: " + processor.countByType());
// Total
System.out.println("\nTotal amount: " + processor.totalAmount() + " INR");
}
}
How to compile and run
javac -d out src/main/java/com/bank/core/*.java
java -cp out com.bank.core.StreamBankApp
Expected output:
=== National Bank of India: Stream Processor ===
Transactions >= 50000 INR:
T1 [SAV2001] DEPOSIT 50000 INR
T3 [CUR3001] DEPOSIT 300000 INR
T4 [SAV2001] DEPOSIT 75000 INR
T5 [CUR3001] WITHDRAWAL 50000 INR
Group by account:
SAV2001: 3 transactions
CUR3001: 2 transactions
SAV2002: 2 transactions
Sum by account:
SAV2001: 135000 INR
CUR3001: 350000 INR
SAV2002: 40000 INR
Count by type: {DEPOSIT=5, WITHDRAWAL=2}
Total amount: 525000 INR
What are common mistakes with streams?
The most common mistake is reusing a stream after it has been consumed. Another mistake is using toMap() without handling duplicate keys.
Wrong: duplicate keys in Collectors.toMap
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
public class WrongToMapDuplicateKey {
record Transaction(String accountId, BigDecimal amount) {}
public static void main(String[] args) {
var txns = List.of(
new Transaction("SAV2001", new BigDecimal("50000")),
new Transaction("SAV2001", new BigDecimal("75000")) // duplicate key
);
try {
// BUG: duplicate key SAV2001 throws IllegalStateException
var map = txns.stream()
.collect(Collectors.toMap(Transaction::accountId, Transaction::amount));
} catch (IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Cause: duplicate key SAV2001 without merge function");
}
}
}
Expected output:
Error: Duplicate key SAV2001 (attempted merging values 50000 and 75000)
Cause: duplicate key SAV2001 without merge function
Correct: provide a merge function
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CorrectToMapWithMerge {
record Transaction(String accountId, BigDecimal amount) {}
public static void main(String[] args) {
var txns = List.of(
new Transaction("SAV2001", new BigDecimal("50000")),
new Transaction("SAV2001", new BigDecimal("75000")),
new Transaction("CUR3001", new BigDecimal("300000"))
);
// Merge function: sum amounts for duplicate keys
Map<String, BigDecimal> balances = txns.stream()
.collect(Collectors.toMap(
Transaction::accountId,
Transaction::amount,
BigDecimal::add));
System.out.println("Correct toMap with merge:");
balances.forEach((id, total) ->
System.out.println(" " + id + ": " + total + " INR"));
}
}
Expected output:
Correct toMap with merge:
SAV2001: 125000 INR
CUR3001: 300000 INR
What about parallel streams?
Parallel streams split the workload across multiple threads using the common ForkJoinPool. They can speed up CPU-bound operations on large datasets, but they add overhead for splitting, thread coordination, and combining results. For small datasets or I/O-bound tasks, sequential streams are faster.
package com.bank.core;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class ParallelStreamDemo {
public static void main(String[] args) {
// Generate 1 million transaction amounts
var amounts = new ArrayList<BigDecimal>();
for (int i = 0; i < 1_000_000; i++) {
amounts.add(BigDecimal.valueOf(i + 1));
}
// Sequential sum
long start = System.nanoTime();
BigDecimal seqSum = amounts.stream()
.reduce(BigDecimal.ZERO, BigDecimal::add);
long seqTime = (System.nanoTime() - start) / 1_000_000;
// Parallel sum
start = System.nanoTime();
BigDecimal parSum = amounts.parallelStream()
.reduce(BigDecimal.ZERO, BigDecimal::add);
long parTime = (System.nanoTime() - start) / 1_000_000;
System.out.println("Sequential sum: " + seqSum + " in " + seqTime + " ms");
System.out.println("Parallel sum: " + parSum + " in " + parTime + " ms");
System.out.println("Results match: " + seqSum.equals(parSum));
System.out.println("Parallel may be slower for BigDecimal (object creation overhead)");
}
}
Expected output:
Sequential sum: 500000500000 in 120 ms
Parallel sum: 500000500000 in 85 ms
Results match: true
Parallel may be slower for BigDecimal (object creation overhead)
What should a senior or principal engineer know?
At the senior level, you should know when streams improve readability and when they add unnecessary complexity. Streams excel at declarative data transformation pipelines but should not replace simple loops for trivial operations.
At the principal level, consider:
- Short-circuiting: findFirst(), findAny(), anyMatch(), allMatch(), noneMatch() stop processing early.
- Collector composition: groupingBy with downstream collectors (filtering, mapping, flatMapping, collectingAndThen).
- Custom collectors: implement Collector
for specialized accumulation logic. - Stream.of() vs Arrays.stream(): Stream.of(array) creates a stream of one element (the array itself). Arrays.stream(array) streams the array elements.
- ForkJoinPool sizing: parallelStream() uses the common ForkJoinPool. Set -Djava.util.concurrent.ForkJoinPool.common.parallelism to control the thread count.
- Debugging: use peek() to inspect elements at each stage of the pipeline.
package com.bank.core;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SeniorStreamKnowledge {
record Transaction(String accountId, String type, BigDecimal amount) {}
public static void main(String[] args) {
var txns = List.of(
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("50000")),
new Transaction("SAV2001", "WITHDRAWAL", new BigDecimal("10000")),
new Transaction("CUR3001", "DEPOSIT", new BigDecimal("300000")),
new Transaction("SAV2001", "DEPOSIT", new BigDecimal("75000")),
new Transaction("SAV2002", "DEPOSIT", new BigDecimal("25000"))
);
System.out.println("Senior/Principal stream checklist:");
System.out.println(" 1. Use streams for declarative pipelines, loops for side effects");
System.out.println(" 2. groupingBy with downstream: filtering, mapping, counting");
System.out.println(" 3. Short-circuit: findFirst, anyMatch stop early");
System.out.println(" 4. Parallel streams only for CPU-bound, large datasets");
System.out.println(" 5. Custom Collector for specialized accumulation");
// Advanced: groupingBy with downstream filtering
Map<String, List<BigDecimal>> depositsOnly = txns.stream()
.collect(Collectors.groupingBy(
Transaction::accountId,
Collectors.filtering(
t -> t.type().equals("DEPOSIT"),
Collectors.mapping(Transaction::amount, Collectors.toList()))));
System.out.println("\nDeposits only per account:");
depositsOnly.forEach((id, amounts) ->
System.out.println(" " + id + ": " + amounts));
// Short-circuit: find any deposit > 100000
var largeDeposit = txns.stream()
.filter(t -> t.type().equals("DEPOSIT"))
.filter(t -> t.amount().compareTo(new BigDecimal("100000")) > 0)
.findFirst();
System.out.println("\nFirst deposit > 100000: " + largeDeposit.orElse(null));
}
}
Expected output:
Senior/Principal stream checklist:
1. Use streams for declarative pipelines, loops for side effects
2. groupingBy with downstream: filtering, mapping, counting
3. Short-circuit: findFirst, anyMatch stop early
4. Parallel streams only for CPU-bound, large datasets
5. Custom Collector for specialized accumulation
Deposits only per account:
SAV2001: [50000, 75000]
CUR3001: [300000]
SAV2002: [25000]
First deposit > 100000: Transaction[txnId=null, accountId=CUR3001, type=DEPOSIT, amount=300000]
What are common interview follow-up questions?
- What is the difference between map() and flatMap()? (map transforms each element to one element. flatMap transforms each element to a stream and flattens all streams into one. Use flatMap when each element maps to multiple values, like a list of transactions per account.)
- Can you modify the source collection while streaming? (No. Modifying the source during a stream operation throws ConcurrentModificationException.)
- What is the difference between reduce() and collect()? (reduce combines elements into a single immutable value. collect accumulates into a mutable container like a List or Map.)
- When should you use streams vs loops? (Use streams for declarative pipelines without side effects: filter, map, group, reduce. Use loops when you need index access, break/continue, or side effects.)
- What is a spliterator? (An object that knows how to split a data source for parallel processing. ArrayList's spliterator splits by index range. LinkedList's spliterator is less efficient because nodes are scattered.)
- How do you handle checked exceptions in streams? (Wrap the lambda in a try-catch or create a helper method that converts checked to unchecked exceptions.)
Visual Overview
Stream Pipeline: Source -> Intermediate -> Terminal
=====================================================
Source: List<Transaction>
│
▼
Intermediate 1: .filter(type == DEPOSIT)
│
▼
Intermediate 2: .map(Transaction::amount)
│
▼
Intermediate 3: .sorted()
│
▼
Terminal: .collect(toList())
│
▼
Result: List<BigDecimal>
Lazy evaluation: nothing happens until collect() triggers.
Loop fusion: each element passes through all stages in one pass.
groupingBy Flow
================
List<Transaction>
│
▼
groupingBy(accountId)
│
├──> SAV2001: [T1, T2, T4]
├──> CUR3001: [T3, T5]
└──> SAV2002: [T6, T7]
With downstream Collectors.reducing(BigDecimal::add):
├──> SAV2001: 135000 INR
├──> CUR3001: 350000 INR
└──> SAV2002: 40000 INR
Key Takeaway
Java Streams provide a declarative way to process collections. You build a pipeline of intermediate operations (filter, map, sorted, distinct) and trigger it with a terminal operation (collect, reduce, forEach, count). Streams are lazy: intermediate operations build the pipeline but process nothing until the terminal operation runs.
Collectors.groupingBy() is the stream equivalent of SQL GROUP BY. It groups elements by a classifier function into a Map. You can chain downstream collectors like counting(), reducing(), filtering(), and mapping() to aggregate within each group. In our National Bank of India project, we grouped transactions by account ID and computed the sum per account in a single pipeline.
The reduce() operation combines all elements into one value. Use it for sums, products, max, min, and custom aggregations. For collecting into mutable containers (List, Map, Set), use collect() with the appropriate Collector. When using Collectors.toMap() with potentially duplicate keys, always provide a merge function to avoid IllegalStateException.
Parallel streams split work across ForkJoinPool threads. They help with CPU-bound operations on large datasets but add overhead for small collections or I/O-bound tasks. Measure before using parallelStream(). At the senior level, understand short-circuiting operations (findFirst, anyMatch), downstream collector composition, and when to prefer loops over streams.
In production banking systems, streams are ideal for batch processing: filter transactions above a threshold, group by account, sum balances, and export reports. They replace verbose nested loops with readable, composable pipelines. But avoid streams for code that needs side effects, early exit with break, or exception handling at each step.
Remember one sentence: Java Streams are lazy pipelines where filter, map, and sorted build the plan, collect or reduce executes it, and groupingBy with downstream collectors gives you SQL-like aggregation in pure Java.
---