Introduction to Streams

🚀 What Are Streams?

A Stream in Java is a sequence of data elements supporting sequential and parallel aggregate operations.

Simply put, Streams let you process data declaratively — what to do, not how to do it.

They bring a functional…


This content originally appeared on DEV Community and was authored by Priyank Bhardwaj

🚀 What Are Streams?

A Stream in Java is a sequence of data elements supporting sequential and parallel aggregate operations.

Simply put, Streams let you process data declaratively — what to do, not how to do it.

They bring a functional programming flavor to Java.

You can think of a Stream as a pipeline of operations that transform data step by step.

Key points:

  • Streams don’t store data — they operate on existing data sources (like Collections, Arrays, or I/O channels).
  • They are lazy — computation happens only when a terminal operation is invoked.
  • They are functional — once used, a Stream cannot be reused.

🆚 Collections vs Streams

Feature Collections Streams
Nature Data structure (stores elements) Data pipeline (processes elements)
Iteration External (you control iteration with loops) Internal (handled by Stream API)
Reusability Can be reused Consumed once
Evaluation Eager (stores all elements) Lazy (computes on demand)
Example List<Integer> list = Arrays.asList(1,2,3); list.stream().filter(n -> n>1)

👉 In short: Collections are about data storage, Streams are about data processing.

Lambda Notation Refresher

Before Streams, we’d typically write something like:

for (String name : names) {
    if (name.startsWith("A")) {
        System.out.println(name);
    }
}

With Lambdas and Streams, it becomes:

names.stream()
     .filter(name -> name.startsWith("A"))
     .forEach(System.out::println);

See the difference?
No loops, no boilerplate — just intent.

A lambda expression is essentially a short block of code that takes input and returns output, often used as an argument to Stream methods like filter(), map(), and forEach().

The Stream Pipeline

Every Stream operation flows through three main stages:

1. Source

Where data originates — e.g., a collection, array, or file.

Stream<String> stream = List.of("Java", "Python", "C++").stream();

2. Intermediate Operations

Transform or filter data.
These are lazy — they don’t do anything until a terminal operation is called.

Examples:
filter(), map(), sorted(), distinct(), limit()

stream.filter(lang -> lang.startsWith("J"))
      .map(String::toUpperCase);

3. Terminal Operations

Trigger the actual processing and produce a result.

Examples:
forEach(), collect(), count(), findFirst()

long count = List.of("Java", "Python", "C++")
                 .stream()
                 .filter(lang -> lang.startsWith("J"))
                 .count();
System.out.println(count); // Output: 1

Think of it as:

Source → Intermediate Operations → Terminal Operation

Simple Examples to Get Started

Example 1: Filtering and Printing

List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6);

numbers.stream()
       .filter(number -> number % 2 == 0)
       .forEach(System.out::println);

// Output: 2 4 6

Example 2: Mapping to Another Type

List<String> words = List.of("java", "stream", "api");

List<String> upper = words.stream()
                          .map(String::toUpperCase)
                          .toList();

System.out.println(upper); // [JAVA, STREAM, API]

Example 3: Combining Operations

List<String> names = List.of("Ayush", "Raj", "Anita", "Vikram");

long count = names.stream()
                  .filter(name -> name.startsWith("A"))
                  .map(String::toUpperCase)
                  .count();

System.out.println(count); // 2

Coming Up Next

In upcoming parts, we’ll deep dive into Intermediate Operations like:

filter(), map(), flatMap(), distinct(), sorted(), and more with real-world use cases and performance insights.

But before we go deeper into Stream operations, we need to understand the functional interfaces that make all this possible.

We'll explore:

  • Predicate, Function, Consumer, Supplier, and BiFunction
  • How they power Stream methods
  • When and how to create your own functional interfaces

Stay tuned and follow to get notified when the next part drops!

đź”— Next Up (Part 2): Functional Interfaces You Must Know


This content originally appeared on DEV Community and was authored by Priyank Bhardwaj


Print Share Comment Cite Upload Translate Updates
APA

Priyank Bhardwaj | Sciencx (2025-10-11T12:35:31+00:00) Introduction to Streams. Retrieved from https://www.scien.cx/2025/10/11/introduction-to-streams/

MLA
" » Introduction to Streams." Priyank Bhardwaj | Sciencx - Saturday October 11, 2025, https://www.scien.cx/2025/10/11/introduction-to-streams/
HARVARD
Priyank Bhardwaj | Sciencx Saturday October 11, 2025 » Introduction to Streams., viewed ,<https://www.scien.cx/2025/10/11/introduction-to-streams/>
VANCOUVER
Priyank Bhardwaj | Sciencx - » Introduction to Streams. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/11/introduction-to-streams/
CHICAGO
" » Introduction to Streams." Priyank Bhardwaj | Sciencx - Accessed . https://www.scien.cx/2025/10/11/introduction-to-streams/
IEEE
" » Introduction to Streams." Priyank Bhardwaj | Sciencx [Online]. Available: https://www.scien.cx/2025/10/11/introduction-to-streams/. [Accessed: ]
rf:citation
» Introduction to Streams | Priyank Bhardwaj | Sciencx | https://www.scien.cx/2025/10/11/introduction-to-streams/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.