Skip to content

Chapter 27 of 37

Lambda Expressions

Create target-typed behavior with captures, method references, and local functions.

34 minutes 10 quick checksBy Subha Prasad
Lesson 27 of 37Course navigation

Lesson content

Read, practise, then check your understanding

A lambda implements the one abstract method of a target functional interface. Parameter types can usually be inferred.

Local behavior

int minimum = 5;
Predicate<String> longEnough = text -> text.length() >= minimum;
names.removeIf(String::isBlank);
names.sort(String::compareToIgnoreCase);

Captured locals must be final or effectively final. Capturing mutable objects still allows their mutation, which can complicate concurrency. Method references are concise when they remain obvious. Lambdas do not introduce an enclosing this like anonymous classes. Prefer short, side-effect-light lambdas; name complex logic for testing and reuse.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes lambda expression?
Which Java term matches this description: A compact implementation of a functional interface method.
Which statement best describes capture?
Which Java term matches this description: Use of an effectively final local variable from enclosing scope.
Which statement best describes method reference?
Which Java term matches this description: A callable reference such as String::length.
Which statement best describes target typing?
Which Java term matches this description: Deriving a lambda's type from its surrounding functional-interface context.
Which statement best describes stateless lambda?
Which Java term matches this description: A lambda that captures no enclosing local values.

0 of 10 checks passed

Your progress is saved on this device.

Lambda Expressions | Java Lesson | Subha Prasad