Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Lambda Expressions in Java 8

Overview

Lambda expressions, a key feature introduced in Java 8, provide a clear and concise way to represent one method interface using an expression. They help to reduce the amount of boilerplate code in the codebase, especially in cases of anonymous class implementations.

Syntax

The basic syntax of a lambda expression is:

(parameters) -> expression

or

(parameters) -> { statements }

Examples

Example 1: Using Lambda Expression to Implement a Runnable

Traditional way:

Runnable r1 = new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello, World!");
    }
};

With lambda expression:

Runnable r2 = () -> System.out.println("Hello, World!");

Example 2: Using Lambda Expression with Functional Interface

Creating a functional interface:

@FunctionalInterface
interface MyFunctionalInterface {
    void myMethod();
}

Implementing the interface using lambda expression:

MyFunctionalInterface func = () -> System.out.println("Functional Interface Example");
func.myMethod();

Example 3: Using Lambda Expression for Event Handling

Traditional way:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button clicked!");
    }
});

With lambda expression:

button.addActionListener(e -> System.out.println("Button clicked!"));

Lambda Expressions and Collections

Lambda expressions can be particularly useful when working with collections. They can simplify the code required for operations such as filtering, mapping, and iterating over collections.

Example: Using Lambda Expressions with Collections

Using forEach method with lambda expression:

List names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));

Using filter method with lambda expression:

List names = Arrays.asList("Alice", "Bob", "Charlie", "David");
names.stream()
     .filter(name -> name.startsWith("A"))
     .forEach(name -> System.out.println(name));

Method References

Method references provide a way to refer to a method without invoking it. They complement lambda expressions and provide an easy-to-read alternative for complex lambda expressions. The types of method references are:

  • Reference to a static method: ClassName::staticMethodName
  • Reference to an instance method of a particular object: object::instanceMethodName
  • Reference to an instance method of an arbitrary object of a particular type: ClassName::instanceMethodName
  • Reference to a constructor: ClassName::new

Example: Using Method References

Using static method reference:

List numbers = Arrays.asList(3, 2, 1);
numbers.sort(Integer::compareTo);
numbers.forEach(System.out::println);

Conclusion

Lambda expressions and method references in Java 8 provide powerful tools for writing clean, concise, and expressive code. They significantly reduce boilerplate code, especially when working with collections and functional interfaces.