Skip to content

Instantly share code, notes, and snippets.

@lgsantiago
Last active May 10, 2018 00:16
Show Gist options
  • Select an option

  • Save lgsantiago/5bfa2f983b35fba62f3c8e3353cd5709 to your computer and use it in GitHub Desktop.

Select an option

Save lgsantiago/5bfa2f983b35fba62f3c8e3353cd5709 to your computer and use it in GitHub Desktop.
interface MyGeneric<T> {
T compute(T t);
}
public static void main(String args[]){
// String version of MyGenericInteface
MyGeneric<String> reverse = (str) -> {
String result = "";
for(int i = str.length()-1; i >= 0; i--)
result += str.charAt(i);
return result;
};
// Integer version of MyGeneric
MyGeneric<Integer> factorial = (Integer n) -> {
int result = 1;
for(int i=1; i <= n; i++)
result = i * result;
return result;
};
// Output: omeD adbmaL
System.out.println(reverse.compute("Lambda Demo"));
// Output: 120
System.out.println(factorial.compute(5));
}
@nilendud
Copy link

nilendud commented Feb 28, 2018

Have you tried running this code ?

The interface definition should be like
interface MyGeneric<T> {
T compute(T t);
}

One more thing :
Integer i is never used. You have always set n = 5. Even if you pass 10, the answer is 120.
Remove the line "int n = 5;" and change "Integer i" to "Integer n"

@lgsantiago
Copy link
Author

This is fixed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment