Phase XI
Functional Java
Lambdas, method references, streams, collectors, and Optional.
Use functional interfaces to pass operations as values through lambdas and method references. Build lazy stream pipelines, collect results, represent missing results with Optional, and evaluate when primitive or parallel streams are useful.
When you finish this phase you can
- Explain how a functional-interface value lets a method receive an operation
- Recognise a functional interface and explain what makes it one
- Read and write lambda expressions from their parameter and return contracts
- Build a comparator out of parts instead of writing a compare method
- Explain stream laziness and the role of a terminal operation
- Use Optional as a return type without turning it into a null check
- Identify workloads where parallel streams are likely to help or add overhead
- 11.1Lambdas and Functional InterfacesReplace small anonymous-class implementations with lambda expressions and match each lambda to a functional interface method.Lambda
- 11.2Method References and the Four ShapesUse Function, Predicate, Supplier, and Consumer, then replace eligible lambdas with the four forms of method reference.Method referenceFunction, Predicate, Supplier, Consumer
- 11.3Streams, and When the Work HappensBuild a stream pipeline, distinguish intermediate from terminal operations, and predict how laziness and short-circuiting control execution.Stream
- 11.4Stream Operations and CollectorsChoose transformation, filtering, ordering, reduction, matching, and collection operations from the result your pipeline must produce.Collector
- 11.5OptionalRepresent an absent result explicitly, transform present values safely, and avoid Optional patterns that recreate null checks or hide errors.Optional
- 11.6Parallel and Primitive StreamsUse IntStream, LongStream, and DoubleStream to avoid boxing, then decide when parallel execution is safe and likely to help.Parallel stream