Java lambda with more than 1 parameters

Published by user on

In this post, we will look at how to pass more than 1 parameter to the functional interface using lambda.

There is a provision in Java library for passing one or two parameters using an in-built interface, but there are cases where the user wants to pass more than two parameters to the functional interface.

In such a case we go with the user-defined interface.

Let’s look at how to pass multiple parameters using lambda.

@FunctionalInterface
interface Function3<One, Two, Three, Four> {
	public Four apply(One one, Two two, Three three);
}

public class MultipleParameterLambda {
	public static void main(String[] args) throws Exception {
		Function3<String, Integer, Double, Float> func = (a, b, c) -> 2.0f;
	}
}

As we see we passed String, Integer, Double, and Float and then used them in the method. Now from here on you can use the passed data types as you want.

In java, you cannot pass a varying number of type parameters.

Categories: Java