Dung (Donny) Nguyen

Senior Software Engineer

Lambda Expressions

Lambda Expressions are one of the most significant features introduced in Java 8, allowing us to write more concise and readable code. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions are particularly useful for enhancing the efficiency of the Java programming language, especially when dealing with collections and streams.

Key Features of Lambda Expressions

Syntax

The syntax of a lambda expression is as follows:

(parameters) -> expression
// or
(parameters) -> { statements; }

Examples

Example 1: Using Lambda Expressions with a Simple Interface

Let’s say we have a functional interface (an interface with a single abstract method) called GreetingService.

@FunctionalInterface
interface GreetingService {
    void sayMessage(String message);
}

// Using lambda expression to define the method
GreetingService greetService = message -> System.out.println("Hello " + message);
greetService.sayMessage("World");

In this example:

Example 2: Using Lambda Expressions with Collections

Lambda expressions can be used to simplify operations on collections, such as filtering, mapping, and reducing.

import java.util.Arrays;
import java.util.List;

public class LambdaExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");

        // Using lambda expression to print each name
        names.forEach(name -> System.out.println(name));

        // Using lambda expression to filter and print names starting with 'A'
        names.stream()
             .filter(name -> name.startsWith("A"))
             .forEach(name -> System.out.println("Name starting with A: " + name));
    }
}

In this example:

Advantages

Practical Use Cases

Lambda expressions in Java 8 represent a major step towards functional programming in Java, making it easier to write and maintain code.