Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 45

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.

💡 A stream pipeline = source → intermediate steps (filter, map) → terminal step (collect, forEach).

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));
1
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);
2
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);
1
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);
[2, 4]

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);
[KABIR]

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.
  • filter keeps some items; map transforms each.
  • collect gathers 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?
The Stream API, added in Java 8, lets you process collections in a clean, functional style — filtering, transforming and collecting data without writing loops. You chain operations into a pipeline that reads like a description of what you want.
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.
What is the difference between intermediate and terminal operations?
Intermediate operations (filter, map) return a new stream and are lazy — they do nothing until a terminal operation runs. Terminal operations (collect, forEach, count) produce a result and trigger the whole pipeline.
Are streams a replacement for loops?
Streams are often cleaner than loops for filtering and transforming data, but not always. For simple iteration or side effects, a loop can be clearer. Use streams where they make the intent more readable.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicGarbage Collection Your progress: 45 / 61 (74%) · Lesson 46 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free