Last active
December 12, 2015 07:59
-
-
Save jweden/4741038 to your computer and use it in GitHub Desktop.
Demonstration of the power of java 8 lambdas with generics.
This file contains 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
public interface FunctionN<T,R> { | |
R apply(T...t); | |
} |
This file contains 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
public class Main { | |
public static void main(String args[]) { | |
new Main().doPrintln() ; | |
} | |
void doPrintln() { | |
FunctionN myFunc1 = (params) -> { | |
StringBuilder sb = new StringBuilder(params.length); | |
for (Object s: params) { | |
sb.append(" " + s); | |
} | |
return "hello" + sb; | |
}; | |
//prints "hello there" | |
System.out.println(myFunc1.apply("there")); | |
//prints "hello there now" | |
System.out.println(myFunc1.apply("there", "now")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment