Command Palette

Search for a command to run...

Phase 10Intermediate11 of 17 in Core Java

Lambdas and Streams

Pass behaviour as values: lambdas, functional interfaces, method references, streams, collectors, Optional and parallel streams, down to invokedynamic.

Until Java 8, the only way to hand a piece of behaviour to a method ("sort by this rule", "run this later", "keep only these") was to wrap it in an object, usually an anonymous class of five or more lines. Lambdas made behaviour a value you can write in one line, and streams built on them a way to describe data processing as a pipeline: take these orders, keep the paid ones, group them by city, add up the totals. This phase is where Java code starts to read like a description of *what* you want instead of a list of loop steps.

You'll learn exactly what a lambda is and what it can capture, the functional interfaces in java.util.function, the four kinds of method reference, and then streams from the ground up: how a pipeline is built, why nothing runs until a terminal operation, which operations are stateless, stateful or short-circuiting, how collectors group and summarise data, how Optional replaces null returns, and when parallel streams help or hurt. Every topic also opens the hood: invokedynamic, LambdaMetafactory, spliterators, the fork/join pool.

This phase builds on interfaces and default methods (Topics 5.7–5.8), anonymous classes (Topic 5.13), generics and wildcards (Phase 8) and the collections framework (Phase 9). Concurrency (Phase 13) returns to the fork/join pool behind parallel streams, and many DSA course solutions (/dsa) use the stream idioms you learn here.

0/8 · 0%
8 topics ~4 h 35 code blocks & diagrams
Start with the first topic
1
10.1

Lambda Expressions

A lambda expression is a short, nameless function you can store in a variable or pass to a method, like (a, b) -> a + b. It always becomes an object of a functional interface type, and it can read local variables only if they are effectively final.

34 min 4 code practice

2
10.2

Functional Interfaces

A functional interface is an interface with exactly one abstract method, and it is the only kind of type a lambda can become. java.util.function provides the standard shapes (Supplier, Consumer, Function, Predicate and friends), plus primitive versions that avoid boxing.

32 min 4 code practice

3
10.3

Method References

A method reference like String::length or System.out::println is a shorter way to write a lambda that only calls one existing method. There are four kinds: static, bound instance, unbound instance and constructor references.

30 min 4 code practice

4
10.4

Streams: The Basics

A stream is a pipeline that pulls elements from a source, passes them through intermediate operations like filter and map, and produces a result with one terminal operation. Streams are lazy (nothing runs until the terminal operation), can be used only once, and never change their source.

31 min 5 code practice

5
10.5

Stream Operations in Depth

Stream operations fall into clear groups: stateless intermediate operations (filter, map, flatMap), stateful ones that must remember or buffer elements (distinct, sorted, limit), short-circuiting ones that can stop early (limit, anyMatch, findFirst), and terminal operations like reduce, min and toArray. Knowing which is which tells you what a pipeline costs and whether it can finish.

34 min 5 code practice

6
10.6

Collectors

A collector is a recipe that tells stream.collect(...) how to build a result: a list, a set, a joined string, a map, or a map of groups with counts and totals. Collectors provides ready-made ones like toMap, groupingBy, partitioningBy, joining and teeing, and they can be nested to answer questions like "total sales per city" in one pipeline.

34 min 5 code practice

7
10.7

Optional

Optional<T> is a box that holds either one value or nothing, used as a method's return type to say "there may be no result". Instead of returning null and hoping the caller checks, you return an Optional and the caller must decide what happens when it's empty, with orElse, orElseThrow, map and friends.

31 min 4 code practice

8
10.8

Parallel Streams and Their Pitfalls

Calling parallelStream() or .parallel() splits a stream's work across the threads of the shared fork/join pool. It gives the same answer as a sequential stream only if your operations are stateless, non-interfering and associative, and it's faster only for large, CPU-heavy work on sources that split well.

34 min 4 code practice