Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, what type represents a function or lambda expression that takes 3 parameters?

Java 8 has Supplier<T> for 0 parameter functions, Function<T, R> for 1 parameter functions, and BiFunction<T, U, R> for 2 parameter functions.

What type represents a function or lambda expression that takes 3 parameters like in

SomeType lambda = (int x, double y, String z) -> x; // what is SomeType?

In C#, Func<T, TResult> is overloaded for up to 16 parameters so we could write

Func<int, double, string, int> lambda = (int x, double y, string z) => x;

Does the Java standard libraries provide anything similar or do you have to write your own "TriFunction" interface to handle functions with 3 arguments?

like image 580
Siqi Lin Avatar asked Mar 11 '15 05:03

Siqi Lin


1 Answers

You have to write your own functional interface. The JDK does not provide such an implementation.

Note that you can always compose something like a TriFunction with existing types

Function<Integer, Function<Double, Function<String, Integer>>> func =
  i -> d -> s -> i + d.intValue() + Integer.parseInt(s);//whatever random implementation
func.apply(42).apply(1.5).apply("1234");
like image 74
Sotirios Delimanolis Avatar answered Oct 10 '22 09:10

Sotirios Delimanolis