Skip to content

Instantly share code, notes, and snippets.

@soverby
Created May 17, 2018 11:25
Show Gist options
  • Save soverby/ba620783a1b165830a620d479798111e to your computer and use it in GitHub Desktop.
Save soverby/ba620783a1b165830a620d479798111e to your computer and use it in GitHub Desktop.
Basic Java Lambda Syntax Variants
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class JavaLambdaSyntax {
private Consumer<String> stringConsumer;
private Supplier<String> stringSupplier;
private BiFunction<String, String, String> stringBiFunction;
public void zeroParamLambda() {
// zero parameters -> parameter parentheses required
// one line function -> no return statement required
stringSupplier = () -> "Blah";
}
public void singleParamLambda() {
// one parameter -> no parameter parentheses required
// one line function -> no return statement required
stringConsumer = stringParam -> System.out.println(stringParam);
}
public void doubleParamLambdaOneLine() {
// > 1 parameter -> parentheses required
stringBiFunction = (param1, param2) -> param1.concat(param2);
}
public void methodExpression() {
// Same as singleParamLambda but use method reference which is syntactic sugar
stringConsumer = System.out::println;
}
public void multiLineExpression() {
stringSupplier = () -> {
System.out.println("do some work");
return "blah";
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment