Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Advanced Java Enterprise & Concurrency 20 mins read Free Lesson

Java Streams API & Functional Programming

Master declarative data pipelines using Stream.filter(), map(), flatMap(), reduce(), and Collectors.

The Java Streams API

The Streams API (introduced in Java 8 and continuously refined) enables functional, declarative operations over sequences of elements.

import java.util.List;
import java.util.stream.Collectors;

public class StreamPipelineDemo {
    public static void main(String[] args) {
        List<String> technologies = List.of("Java", "Spring Boot", "Docker", "Kubernetes", "Kafka");

        // Filter, transform, and collect
        List<String> filtered = technologies.stream()
            .filter(tech -> tech.startsWith("K") || tech.startsWith("S"))
            .map(String::toUpperCase)
            .sorted()
            .collect(Collectors.toList());

        System.out.println(filtered); // [KAFKA, KUBERNETES, SPRING BOOT]
    }
}

Practice Exercises

Exercise 1: Filter and Map Stream Elements

Easy

Create a stream pipeline that filters even numbers and squares them.

Starter Code:
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);
// stream pipeline here
💡 Hint: Use .filter(n -> n % 2 == 0).map(n -> n * n).toList()
Solution:
List<Integer> squares = nums.stream().filter(n -> n % 2 == 0).map(n -> n * n).toList();