Skip to main content
Module 7 · Lesson 1 of 215/22 in the course~15 min
Module lessons (1/2)

Lambda Expressions and Functional Interfaces

Lesson preview5 hands-on exercises readyCourse Java · Module 7 — Lambda and Stream API

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, Consumer and Supplier.
  • 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

  1. 11. From Anonymous Classes to Lambda Syntax
  2. 22. The Four Fundamental Interfaces of java.util.function
  3. 33. Method Reference (::)
  4. 44. Captured and Effectively Final Variables
  5. 5Try it yourself
Pro Lesson · Module 7 — Lambda and Stream API

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.