Java

Stream API

Process data easily. Filter, map, and reduce collections in a functional style.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Process data easily. Filter, map, and reduce collections in a functional style. This hands-on tutorial focuses on practical implementation of stream api concepts.

Stream API

A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

[!NOTE] A Stream is not a data structure. It takes input from Collections, Arrays, or I/O channels.

Key Operations

1. Intermediate Operations (Returns a Stream)

  • filter(): Selects elements based on a condition.
  • map(): Transforms elements.
  • sorted(): Sorts the stream.

2. Terminal Operations (Returns a Result)

  • collect(): Gathers results back into a Collection.
  • forEach(): Iterates over elements.
  • reduce(): Reduces elements to a single value.
  • count(): Counts the elements.

Example Usage

List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");

// Filter names starting with 'C'
List<String> result = names.stream()
    .filter(name -> name.startsWith("C"))
    .collect(Collectors.toList());

System.out.println(result); // [Charlie]

Interactive Code

Analyze a list of numbers!

JAVA PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Java Stream API: filter, map, reduce and processing collections"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

Does a Stream modify the source collection?

Yes
No

Next Steps

NullPointerExceptions are the worst. Java 8 gave us a weapon against them: Optional Class.