Use cases of lambda expressions in Java
  1. Functional Interfaces: Lambda expressions are extensively used with functional interfaces, which are interfaces that have only one abstract method. By using lambda expressions, you can implement the abstract method directly in-line, without the need for a separate class or anonymous inner class. This simplifies the code and makes it more readable.

Example:

// Functional interface with a single abstract method
interface Calculator {
    int calculate(int a, int b);
}

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression implementing the Calculator interface
        Calculator add = (a, b) -> a + b;
        System.out.println(add.calculate(5, 3)); // Output: 8
        
        Calculator multiply = (a, b) -> a * b;
        System.out.println(multiply.calculate(5, 3)); // Output: 15
    }
}
  1. List Manipulation: Lambda expressions can be used for manipulating lists or collections. They provide a concise way to iterate over elements and perform operations such as filtering, mapping, and reducing.

Example:

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

public class ListManipulationExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        // Filtering even numbers
        numbers.stream()
            .filter(n -> n % 2 == 0)
            .forEach(System.out::println); // Output: 2, 4

        // Mapping numbers to their squares
        numbers.stream()
            .map(n -> n * n)
            .forEach(System.out::println); // Output: 1, 4, 9, 16, 25

        // Reducing the numbers to their sum
        int sum = numbers.stream()
            .reduce(0, (a, b) -> a + b);
        System.out.println(sum); // Output: 15
    }
}
  1. Multithreading: Lambda expressions can be used to simplify multithreading code by providing a concise way to write anonymous Runnable or Callable instances.

Example:

public class MultithreadingExample {
    public static void main(String[] args) {
        // Using lambda expression to create a new thread
        new Thread(() -> {
            System.out.println("Thread is running");
        }).start();
    }
}

These are just a few examples of how lambda expressions can be used in Java. They provide a more functional way of writing code, reducing boilerplate and making your code more expressive and readable. Embracing lambda expressions can greatly enhance the power and flexibility of your Java programs.

References: