简体   繁体   中英

How can we have 2 parameters in java.util.function.Function lambda?

We can create lambda functions like this:

Function<Integer, String> getLambda = (a) -> new String("given value is "a);

I have a scenario where I need to take 2 values in a parameter. How can I accomplish that using Function?

Example:

getLamda(10,20); // I know this line will give error. How can I acheive this? 

This is done using a BiFunction<T,U,R> . Following is an example of a BiFunction returning the character at the specified index of a String:

BiFunction<String, Integer, Character> charAtFunction = (string, index) -> string.charAt(index);

试试:

BiFunction<Integer, Integer, String> lambda = (a, b) -> ("Given values are " + a + ", " + b);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM