Module lessons (1/2)
Lambda Expressions and Functional Interfaces
Predicate, Function, Consumer, Supplier, method reference (Class::method) and constraint effectively final.
What you'll learn in this lesson
- Replace verbose anonymous classes with lambda expressions on any
@FunctionalInterface(Single Abstract Method). - Mastery of the standard interfaces of
java.util.function:Predicate,Function,ConsumerandSupplier. - Use the method reference (
Class::method) and respect the effectively final rule when capturing local variables.
Overview
A lambda expression is a lean anonymous function that implements a Functional Interface on the fly, i.e. an interface with only one abstract method (SAM, annotatable with @FunctionalInterface).
If there are zero () -> 42 parameters, or more (a, b) -> a + b parameters, parentheses become mandatory; if the body has multiple instructions you need braces and explicit return: (a, b) -> { int s = a + b; return s * 2; }.
Sections covered in this lesson
- 11. From Anonymous Classes to Lambda Syntax
- 22. The Four Fundamental Interfaces of
java.util.function - 33. Method Reference (
::) - 44. Captured and Effectively Final Variables
- 5Try it yourself
Unlock "Lambda Expressions and Functional Interfaces" and the full Course Java curriculum
Predicate, Function, Consumer, Supplier, method reference (`Class::method`) and constraint effectively final.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Java plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.