Last active
December 10, 2018 10:10
-
-
Save dfparker2002/b4f0e761fb94a57e2c58cd334d624cde to your computer and use it in GitHub Desktop.
Lambda Function
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
/* 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