Skip to main content
Java Basics
CHAPTER 25 Beginner

Java Streams and Lambda Expressions

Updated: May 17, 2026
5 min read

# CHAPTER 25

Java Streams and Lambda Expressions

1. Introduction

Java 8 revolutionized the language with functional programming features. Lambda expressions provide concise syntax for anonymous functions, and the Streams API enables powerful, declarative data processing pipelines.

2. Lambda Expressions

A shorter way to implement functional interfaces:
java
1234567891011
// Traditional anonymous class
Runnable r1 = new Runnable() {
    @Override
    public void run() { System.out.println("Hello!"); }
};

// Lambda expression
Runnable r2 = () -> System.out.println("Hello!");

// With parameters
Comparator<String> comp = (a, b) -> a.compareTo(b);

Syntax: (parameters) -> expression or (parameters) -> { statements; }

3. Functional Interfaces

Common built-in functional interfaces:
java
1234567891011121314151617
import java.util.function.*;

// Predicate — takes input, returns boolean
Predicate<Integer> isEven = n -> n % 2 == 0;
isEven.test(4); // true

// Function — takes input, returns output
Function<String, Integer> length = s -> s.length();
length.apply("Hello"); // 5

// Consumer — takes input, returns nothing
Consumer<String> printer = s -> System.out.println(s);
printer.accept("Hi!"); // prints "Hi!"

// Supplier — takes nothing, returns output
Supplier<Double> random = () -> Math.random();
random.get(); // 0.73...

4. Streams API

A stream is a pipeline that processes data from a source (collection, array):
java
123456789
import java.util.*;
import java.util.stream.*;

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

// Filter: keep names starting with vowels
names.stream()
     .filter(name -> name.startsWith("A") || name.startsWith("E"))
     .forEach(System.out::println); // Alice, Eve

5. Common Stream Operations

filter(): Keep elements matching a condition

java
123
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evens = nums.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
// [2, 4, 6, 8, 10]

map(): Transform each element

java
12
List<String> upper = names.stream().map(String::toUpperCase).collect(Collectors.toList());
// [ALICE, BOB, CHARLIE, DAVID, EVE]

reduce(): Combine all elements into one

java
1
int sum = nums.stream().reduce(0, Integer::sum); // 55

sorted():

java
1
names.stream().sorted().forEach(System.out::println);

distinct():

java
1
List.of(1, 2, 2, 3, 3, 3).stream().distinct().forEach(System.out::print); // 123

count():

java
1
long count = names.stream().filter(n -> n.length() > 3).count(); // 3

6. Method References

Shorthand for lambdas that call a single method:
java
12
names.forEach(System.out::println);    // equivalent to: n -> System.out.println(n)
names.stream().map(String::toUpperCase); // equivalent to: s -> s.toUpperCase()

7. Collectors

java
12345678910
// Join strings
String joined = names.stream().collect(Collectors.joining(", ")); // "Alice, Bob, ..."

// Group by length
Map<Integer, List<String>> byLength = names.stream()
    .collect(Collectors.groupingBy(String::length));

// Partition (split into two groups)
Map<Boolean, List<Integer>> partitioned = nums.stream()
    .collect(Collectors.partitioningBy(n -> n > 5));

8. MCQ Quiz with Answers

Question 1

Lambda syntax is:

Question 2

Streams are:

Question 3

filter() does what?

Question 4

map() does what?

Question 5

reduce() does what?

Question 6

collect(Collectors.toList()) does what?

Question 7

Method reference String::toUpperCase is equivalent to:

Question 8

A Predicate returns:

Question 9

Streams are:

Question 10

stream().sorted() sorts in:

9. Summary

Lambda expressions provide concise anonymous function syntax. The Streams API enables declarative data processing with filter, map, reduce, and collect. Method references are shorthand for simple lambdas. These features bring functional programming power to Java.

10. Next Chapter Recommendation

In Chapter 26: Java Networking Basics, we'll explore socket programming and HTTP connections.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·