Lambda Expressions Examples

Basic Java programs demonstrating different use cases of Lambda expressions.

Simple Lambda Expression for Addition

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression for adding two numbers
        MathOperation addition = (a, b) -> a + b;
        System.out.println("Addition: " + addition.operation(5, 3));
    }
}

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

Lambda Expression for String Concatenation

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression for string concatenation
        StringOperation concat = (str1, str2) -> str1 + str2;
        System.out.println("Concatenated String: " + concat.operation("Hello", " World"));
    }
}

interface StringOperation {
    String operation(String str1, String str2);
}

Lambda Expression with No Parameters

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression with no parameters
        GreetingService greetService = () -> System.out.println("Hello, Lambda!");
        greetService.sayHello();
    }
}

interface GreetingService {
    void sayHello();
}

Lambda Expression for Finding Maximum of Two Numbers

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression to find the maximum of two numbers
        FindMaximum maxFinder = (a, b) -> a > b ? a : b;
        System.out.println("Maximum: " + maxFinder.findMax(4, 7));
    }
}

interface FindMaximum {
    int findMax(int a, int b);
}

Lambda Expression for Filtering Even Numbers

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

public class LambdaExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        // Lambda expression to filter even numbers
        numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
    }
}

Lambda Expression for Sorting a List

import java.util.*;

public class LambdaExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 2);
        // Lambda expression for sorting a list
        Collections.sort(numbers, (a, b) -> a - b);
        System.out.println("Sorted List: " + numbers);
    }
}

Lambda Expression for Iterating Over a List

import java.util.*;

public class LambdaExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("John", "Jane", "Paul", "Anna");
        // Lambda expression for iterating over the list
        names.forEach(name -> System.out.println(name));
    }
}

Lambda Expression for Calculating Factorial

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression for calculating factorial
        Factorial factorial = n -> {
            int result = 1;
            for (int i = 1; i <= n; i++) {
                result *= i;
            }
            return result;
        };
        System.out.println("Factorial of 5: " + factorial.calculate(5));
    }
}

interface Factorial {
    int calculate(int n);
}

Lambda Expression for String Length Comparison

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression to compare the length of two strings
        StringLengthComparator comparator = (str1, str2) -> str1.length() - str2.length();
        System.out.println("Length Comparison: " + comparator.compare("apple", "banana"));
    }
}

interface StringLengthComparator {
    int compare(String str1, String str2);
}

Lambda Expression with Multiple Statements

public class LambdaExample {
    public static void main(String[] args) {
        // Lambda expression with multiple statements
        GreetMultiple greet = name -> {
            System.out.println("Hello, " + name);
            System.out.println("How are you?");
        };
        greet.sayHello("Alice");
    }
}

interface GreetMultiple {
    void sayHello(String name);
}

These examples showcase the versatility of Lambda expressions in Java, from simple mathematical operations to more complex data manipulation like filtering and sorting.

Scroll to Top