Last active
October 21, 2019 09:50
-
-
Save andreluisdias/992b4df5131ea7e192392c3e477631b8 to your computer and use it in GitHub Desktop.
Example of a Functional TriFunction interface and implementation (with Java 8 Lambdas)
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
package sample.function; | |
/** | |
* Example of TriFunction | |
* | |
* @author Andre Dias | |
* @since 2017-08-12 | |
*/ | |
@FunctionalInterface | |
public interface TriFunction<F, S, T, R> { | |
public R apply(F first, S second, T third); | |
} |
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
package sample.function.tri; | |
/** | |
* @see TriFunction 'Functional Interface' implementation example. | |
* | |
* @author Andre Dias | |
* @since 2017-08-12 | |
*/ | |
public class TriFunctionIntegerAddImpl implements TriFunction<Integer, Integer, Integer, Integer>{ | |
/** | |
* Addition (as default). | |
* | |
* @see sample.function.tri.TriFunction#apply(java.lang.Object, java.lang.Object, java.lang.Object) | |
*/ | |
public Integer apply(Integer first, Integer second, Integer third) { | |
return first.intValue() + second.intValue() + third.intValue(); | |
} | |
public static void main(String[] args) { | |
executeAsUsual(); // Add Result: 6 | |
executeAsLambda(); // Multiply Result: 60 | |
} | |
private static void executeAsUsual() { | |
System.out.println(new TriFunctionIntegerAddImpl().apply(1, 2, 3)); | |
} | |
private static void executeAsLambda() { | |
/* | |
* Create lambda (change default behavior - now multiply) | |
*/ | |
TriFunction<Integer, Integer, Integer, Integer> triMultiply = (a, b, c) -> (a * b * c); | |
/* | |
* Check it | |
*/ | |
System.out.println(triMultiply.apply(10, 2, 3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment