How to use an anonymous function in Java

1 Answer

0 votes
// Anonymous function = A function with no identifier.

import java.util.function.BiFunction;

public class Main {

    public static void main(String[] args) {

        // Anonymous function (lambda) that takes two integers and returns their sum.
        // BiFunction<Integer, Integer, Integer> represents: (a, b) -> result
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

        // Use the anonymous function
        int result = add.apply(10, 20);

        System.out.println(result);
    }
}


/*
run:

30

*/

 



answered 1 day ago by avibootz
...