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)); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter