GENERIC_LAMBDA
Wed May 29 2024 13:19:24 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) -> {
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));
}
}
content_copyCOPY
Comments