Java: Java | Streams - Pipeline Foundations
Let us study Java | Streams like we would in a careful Java review. We will read a small executable example, predict its output, then change one boundary only after we can explain the original contract.
Pipeline Foundations is not a checklist to memorize. It is the part of Java design where clear names, deliberate ownership, and observable failures save us from debugging a vague symptom later.
What We Will Practise Together
We are practising more than syntax. For every example, state what Java guarantees, what the caller can observe, and what should happen when the input is missing, invalid, or late.
- Keep the contract smaller than the explanation around it.
- Use the type system and resource boundaries to make invalid states difficult.
- Test the normal path and the closest failure path together.
- Do not log secrets or pretend a failure is a success.
Example 1: Filter before collecting
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
var ready = List.of("draft", "ready", "ready").stream()
.filter("ready"::equals).toList();
System.out.println(ready.size());
}
}Expected Result
2
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 2: Map one representation to another
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
var labels = List.of("java", "streams").stream()
.map(String::toUpperCase).toList();
System.out.println(labels);
}
}Expected Result
[JAVA, STREAMS]
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 3: Use a named predicate
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> positive = value -> value > 0;
System.out.println(List.of(-1, 2, 3).stream().filter(positive).count());
}
}Expected Result
2
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 4: Reduce with an identity
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
int total = List.of(2, 3, 4).stream().reduce(0, Integer::sum);
System.out.println(total);
}
}Expected Result
9
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 5: Flatten nested input
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
long count = List.of(List.of("a", "b"), List.of("c")).stream()
.flatMap(List::stream).count();
System.out.println(count);
}
}Expected Result
3
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 6: Group by a visible rule
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
var grouped = List.of("ant", "bee", "cat").stream()
.collect(Collectors.groupingBy(String::length));
System.out.println(grouped.get(3).size());
}
}Expected Result
3
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 7: Choose findFirst when order matters
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
String first = List.of("draft", "ready").stream()
.filter("ready"::equals).findFirst().orElse("missing");
System.out.println(first);
}
}Expected Result
ready
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 8: Keep side effects outside the pipeline
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
var values = List.of(1, 2, 3).stream().map(value -> value * 2).toList();
System.out.println(values);
}
}Expected Result
[2, 4, 6]
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 9: Handle an empty stream deliberately
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
int maximum = List.<Integer>of().stream().mapToInt(Integer::intValue).max().orElse(0);
System.out.println(maximum);
}
}Expected Result
0
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Example 10: Review the pipeline in order
Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.
A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.
Java
import java.util.List;
public class Main {
public static void main(String[] args) {
long result = List.of("a", "bb", "ccc").stream()
.filter(value -> value.length() >= 2).map(String::length).count();
System.out.println(result);
}
}Expected Result
2
Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.
Teacher Review
Before we close pipeline foundations, narrate one example without looking at the code. If you can state the input, rule, output, and boundary in order, you have a reusable Java decision rather than an isolated answer.
Now carry one example into a small project. Keep its expected result beside the code while you change it. That is how we build reliable Java judgment one visible contract at a time.
Practice Before You Move On
Choose one snippet and make a small deliberate change. Write the predicted result first, then run it. If the result surprises you, reduce the example until every step is explainable.
