java 8 (Stream API)

Published: (December 15, 2025 at 03:39 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Features of Stream API

  • Declarative – Write concise and readable code using functional style.
  • Lazy Evaluation – Operations are executed only when a terminal operation is needed.
  • Parallel Execution – Supports parallel streams to leverage multi‑core processors.
  • Reusable Operations – Supports chaining of operations like map(), filter(), sorted().
  • No Storage – Streams don’t store data; they only process it.

How Stream works internally?

  1. Create a stream – From collections, arrays, or static methods.
  2. Apply intermediate operations – Transform data (e.g., filter(), map(), sorted()).
  3. Apply terminal operation – Produce a result (e.g., forEach(), collect(), reduce()).

Creation of streams

  • From a Collectionlist.stream() or set.stream().
  • From an ArrayArrays.stream(array).
  • Using Stream.of() – Create a stream from a fixed set of values.
  • Infinite Stream – Generate an unbounded sequence with Stream.iterate() or Stream.generate().

Stream creation diagram

Another creation example

Intermediate operations

Intermediate operations transform a stream into another stream. Common ones include:

  • filter() – Filters elements based on a condition.
  • map() – Transforms each element to another value.
  • sorted() – Sorts the elements.
  • distinct() – Removes duplicates.
  • skip() – Skips the first n elements.

Intermediate operations diagram 1

Intermediate operations diagram 2

Types of streams

Sequential Stream

Processes elements one by one in a single thread. Created by default when you call stream().

Sequential stream diagram 1

Sequential stream diagram 2

Parallel Streams

Can perform operations concurrently on multiple threads, taking advantage of multi‑core CPUs.

Infinite Streams

Generate unbounded sequences. Use limit() to avoid infinite execution.

Infinite stream diagram 1

Infinite stream diagram 2

Back to Blog

Related posts

Read more »