interface MyFunction<T> { T compute(T value); } public class GenericLambda { public static void main(String[] args) { MyFunction<String> reverse = (s) -> { String r = ""; for(int i=s.length()-1; i>=0; i--) r += s.charAt(i); return r; }; 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