Java Functional Essentials – Chapter 1: What Are Java Streams?
T
Tuan Beo

Java Functional Essentials – Chapter 1: What Are Java Streams?

This lesson introduces Java Streams, a modern way to process collections functionally. You'll learn what streams are, why they're useful, and how they compare to traditional loops.

What Is a Stream in Java?

A Stream is a sequence of elements that supports functional-style operations to process data. It's not a data structure itself, but a pipeline for processing data.

Streams allow you to:

  • Transform data (map)

  • Filter data (filter)

  • Aggregate data (reduce, collect)

  • Chain multiple operations in a readable way

Traditional Loop Example

List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
for (String name : names) {
    if (name.startsWith("A")) {
        System.out.println(name.toUpperCase());
    }
}

Stream-Based Equivalent

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

Why Use Streams?

  • Concise: Less boilerplate code.

  • Readable: Clear flow of data.

  • Declarative: Focus on what to do, not how.

  • Parallelizable: Easily switch to .parallelStream() for multithreading.

How Streams Work: The Pipeline

A stream has three steps:

  1. Source: Where the data comes from (List, Set, Array, etc.)

  2. Intermediate Operations: Transformations (map, filter, sorted)

  3. Terminal Operation: Final action (forEach, collect, count)

Example:

List<String> cities = List.of("Berlin", "London", "Paris", "Barcelona");

cities.stream()                     // 1. Source
      .filter(c -> c.length() > 6)  // 2. Intermediate
      .sorted()
      .forEach(System.out::println); // 3. Terminal

Important Notes

  • Streams are lazy: Intermediate operations don’t run until a terminal operation is invoked.

  • Streams are one-time use: Once consumed, you must recreate it to reuse.

  • Streams don't modify the original collection.

Summary

  • A Stream is a powerful tool for processing collections.

  • You define a pipeline of operations to filter, transform, and consume data.

  • Streams help write cleaner, more functional, and declarative code.

Assignment: Lesson 1 – Getting Started with Streams

Objective:

Get hands-on experience using Java Streams to filter, map, and print data.

Part 1: Conceptual Questions

  1. What is a Stream in Java?

  2. What are the three parts of a stream pipeline?

  3. How are Streams different from traditional loops?

Assignment 1 – Easy

Task:

Write a Java program that:

  • Creates a list of names: "Alice", "Bob", "Clara", "Daniel", "Eva"

  • Uses a stream to:

    • Filter names that start with "A" or "C"

    • Convert them to uppercase

    • Print them

Expected Output:

ALICE
CLARA

Assignment 2 – Challenge

Task:

Create a method called filterAndTransform() that:

  • Accepts a List<String> of names

  • Returns a new list of names that:

    • Have length ≥ 5

    • Are reversed ("Charlie""eilrahC")

    • Are sorted in reverse alphabetical order

Hint:

Use map(), filter(), sorted(), and collect(Collectors.toList()).

Sample Output (input: "Anna", "Robert", "Clara", "Eva", "Charlie"):

["treboR", "eilrahC", "aralC"]

Comments