GENERIC LAMBDA

PHOTO EMBED

Wed May 29 2024 16:27:27 GMT+0000 (Coordinated Universal Time)

Saved by @signup

interface MyFunction<T> {
    T compute(T value);
}

public class GenericLambda {
    public static void main(String[] args) {
        MyFunction<String> reverse = s -> new StringBuilder(s).reverse().toString();
        MyFunction<Integer> factorial = n -> {
            int result = 1;
            for (int i = 2; i <= n; i++) result *= i;
            return result;
        };

        System.out.println("Reversed string: " + reverse.compute("Lambda"));
        System.out.println("Factorial: " + factorial.compute(5));
    }
}
content_copyCOPY