14 03 Lambda expression

Lambda expressions were added to the Java language to write small pieces of behaviour in a more compact way. A lambda is similar to a method but you do not need to declare a name for it: you can write the body directly. It can take no parameter, a single parameter without parentheses, or several parameters wrapped in parentheses and separated by commas.

To see this in practice we create a simple list, for example a list of numbers, and we want to print each value. With the new forEach method we pass a lambda: list.forEach(n -> System.out.println(n));. The parameter n represents one element of the list, and the body of the lambda is the block of code that should run for it. This single line replaces the classic for loop with an index.

Storing a lambda in a variable

A lambda is not limited to being passed inline. We can store it in a variable whose type is the functional interface Consumer. For instance: Consumer<Integer> print = n -> System.out.println(n);. Once defined, calling list.forEach(print); produces exactly the same output.

  • No parameter: () -> System.out.println("hello").
  • One parameter (no parentheses needed): n -> System.out.println(n).
  • Multiple parameters (parentheses needed): (a, b) -> a + b.

Lambdas pair very well with collections and streams in Java, because they let us express the operation we want without writing boilerplate iteration code. They are the foundation of more advanced topics like the Streams API, which we will explore later.

Summary

Lambda expressions in Java provide a concise way to implement functional interfaces without creating anonymous inner classes. This lesson covers the syntax variations (single parameters, multiple parameters in parentheses, and code blocks), practical examples using loops, and how to store lambda expressions in variables using the Consumer interface. Lambda expressions simplify code by allowing you to write cleaner, more readable functional implementations directly inline.

Key points

  • Lambda expressions are syntactic shortcuts that eliminate the need for anonymous classes in Java
  • Syntax varies based on parameters: single parameter (no parentheses), multiple parameters (with parentheses), and the arrow (=>) separates parameters from the body
  • Code blocks in lambdas can contain multiple statements; single expressions are evaluated directly
  • The Java Consumer functional interface allows you to store and reuse lambda expressions as variables
  • Lambda expressions integrate seamlessly with for-each loops to iterate and execute code on collections
  • Using lambdas reduces boilerplate code and makes implementations more readable and maintainable

FAQ

What is the basic syntax structure of a lambda expression?

A lambda expression has three parts: (1) parameters (can be single without parentheses or multiple with parentheses), (2) the arrow operator (=>), and (3) the body (single expression or block of statements). Example: n => System.out.println(n) or (a, b) => { return a + b; }

How do I store a lambda expression in a variable?

Use a functional interface type as the variable's type. For example: Consumer<Integer> consumer = n => System.out.println(n); This allows you to reuse the lambda expression multiple times throughout your code.

Why should I use lambda expressions instead of anonymous classes?

Lambda expressions reduce code verbosity, improve readability, and eliminate the boilerplate required by anonymous inner classes. They make functional programming patterns in Java more concise and easier to understand.