Before moving on to the examples below, first note that Lambda expressions can be written for any SAM interfaces (also called functional) (Infact, Lambda expression - syntactic sugar > to replace a detailed anonymous class (with a single method) in Java).
The interface of a single abstract method or a functional interface is an interface that contains only one abstract method), you can see here . If you know this simple point, you can write (play with) any number of your own functional interfaces, and then write various lambda expressions according to each of these functional interface methods.
The examples below are written using existing JDK (1.8) functional interfaces, such as Callable , Function , BiFunction (Similarly, there are many built-in functional interfaces in JDK 1.8, in most cases they meet our requirements ).
(1) Example for (int x, int y) β {return x + y; }
//Below Lamda exp, takes 2 Input int Arguments and returns string BiFunction<Integer, Integer, String> biFunction = (num1, num2) -> "Sum Is:" +(num1 + num2); System.out.println(biFunction.apply(100, 200));
(2) Example for x β x * x
//Below Lamda exp, takes string Input Argument and returns string list.stream().map((String str1) -> str1.substring(0, 1)). forEach(System.out::println);
(3) Example for () β x
//Below Lambda exp, Input argument is void, returns String Callable<String> callabl = () -> "My Callable"; ExecutorService service = Executors.newSingleThreadExecutor(); Future<String> future = service.submit(callable); System.out.println(future.get());