Skip to content

Instantly share code, notes, and snippets.

@jweden
Last active December 12, 2015 07:59
Show Gist options
  • Save jweden/4741038 to your computer and use it in GitHub Desktop.
Save jweden/4741038 to your computer and use it in GitHub Desktop.
Demonstration of the power of java 8 lambdas with generics.
public interface FunctionN<T,R> {
R apply(T...t);
}
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