Skip to content

Instantly share code, notes, and snippets.

@dfparker2002
Last active December 10, 2018 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfparker2002/b4f0e761fb94a57e2c58cd334d624cde to your computer and use it in GitHub Desktop.
Save dfparker2002/b4f0e761fb94a57e2c58cd334d624cde to your computer and use it in GitHub Desktop.
Lambda Function
/* src: java8LambdasVsGroovyClosures/src/main/java/ch/codebulb/java8lambdasvsgroovy/CustomFunctionTestsJava.java
* https://github.com/codebulb/java8LambdasVsGroovyClosures/blob/master/src/main/java/ch/codebulb/java8lambdasvsgroovy/CustomFunctionTestsJava.java
*
*/
import java.util.function.Function;
public class CustomFunctionTestsJava {
protected static int INPUT_VALUE;
protected static String INPUT_STRING;
public static int calculateWithFunction() {
/*
* Note that x is inferred of type Integer. Setting it to int would not compile.
* Rather, you'd have to implement one of the primitive interfaces
*/
Function<Integer, Integer> function = x -> x*2;
return useFunction(function, INPUT_VALUE);
}
private static int useFunction(Function<Integer, Integer> function, int inputValue) {
return function.apply(inputValue);
}
public static char calculateWithMethodReference() {
Function<Integer, Character> function = INPUT_STRING::charAt;
return function.apply(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment