Last active
May 10, 2018 00:16
-
-
Save lgsantiago/5bfa2f983b35fba62f3c8e3353cd5709 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"