Stream API in Java
In this lesson: Process collections with Java Streams — filter, map, sorted, reduce, collect — practical examples with output. In Hindi & English.
What is the Stream API
The Stream API (Java 8) lets you process a collection in a clean, readable way — filtering, transforming and collecting data — without writing manual loops. You build a pipeline of operations that reads almost like plain English.
Creating a stream
import java.util.*;
import java.util.stream.*;
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
nums.stream() // create a stream
.forEach(n -> System.out.println(n));
2
3
4
5
Call .stream() on a collection to start a stream. Here forEach is a terminal operation that runs an action on each item.
filter — keep some
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
nums.stream()
.filter(n -> n % 2 == 0) // keep even numbers
.forEach(System.out::println);
4
6
filter keeps only items where the test is true. It is an intermediate operation, so it returns a new stream you can chain further.
map — transform
List<Integer> nums = Arrays.asList(1, 2, 3);
nums.stream()
.map(n -> n * n) // square each
.forEach(System.out::println);
4
9
map transforms every item using the given function — here squaring each number. The count stays the same; only the values change.
collect — back to a list
List<Integer> nums = Arrays.asList(1, 2, 3, 4);
List<Integer> evens = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evens);
collect(Collectors.toList()) is a terminal operation that gathers the results into a new List, so you can store or return them.
A full pipeline
List<String> names = Arrays.asList("aman", "diya", "kabir");
List<String> result = names.stream()
.filter(n -> n.length() > 4) // keep long names
.map(String::toUpperCase) // make uppercase
.collect(Collectors.toList()); // back to a list
System.out.println(result);
A pipeline chains steps: filter, then map, then collect. It reads top-to-bottom as "keep long names, uppercase them, collect them" — far clearer than nested loops.
Common mistakes
- Forgetting a terminal operation. Without collect/forEach, nothing runs (streams are lazy).
- Reusing a stream. A stream can be used once; create a new one to run again.
- Confusing filter and map. filter selects; map transforms.
- Overusing streams. For simple loops or side effects, a plain loop may read better.
Summary
- The Stream API processes collections without manual loops.
- Start with
.stream(), then chain operations. filterkeeps some items;maptransforms each.collectgathers results back into a list (terminal).- Intermediate steps are lazy; a terminal step runs the pipeline.
Next, write the lambdas streams use in lambda expressions, or store results in a collection.
Frequently Asked Questions
What is the Stream API in Java?
What is the difference between filter and map?
filter keeps only the items that pass a test, so it can return fewer items. map transforms every item into something new, keeping the same count. You often use them together.What does collect do in a stream?
collect is a terminal operation that gathers the stream results back into a collection, like a List, using Collectors.toList(). It ends the pipeline and gives you a usable result.