Skip to content

Instantly share code, notes, and snippets.

@andreluisdias
Last active October 21, 2019 09:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreluisdias/992b4df5131ea7e192392c3e477631b8 to your computer and use it in GitHub Desktop.
Save andreluisdias/992b4df5131ea7e192392c3e477631b8 to your computer and use it in GitHub Desktop.
Example of a Functional TriFunction interface and implementation (with Java 8 Lambdas)
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);
}
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