Lambdas

Lambdas in Java are anonymous functions that can be treated as objects and passed around. Introduced in Java 8, they enable functional programming features such as concise function representations and behavior passing.


Syntax

(parameters) -> expression
(parameters) -> { statements }

Example

interface Greeting {
    void sayHello();
}

public class Main {
    public static void main(String[] args) {
        Greeting g = () -> System.out.println("Hello, Lambda!");
        g.sayHello();
    }
}

With Parameters

interface MathOperation {
    int operation(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        MathOperation add = (a, b) -> a + b;
        System.out.println(add.operation(5, 3)); // Output: 8
    }
}

Use with Collections

List<String> list = Arrays.asList("A", "B", "C");
list.forEach(item -> System.out.println(item));

Benefits

  • Shorter code
  • Improved readability
  • Enables functional-style programming