21

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? 
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54

2 Answers2

37

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);
Tunaki
  • 132,869
  • 46
  • 340
  • 423
8

Try :

BiFunction<Integer, Integer, String> lambda = (a, b) -> ("Given values are " + a + ", " + b);
dotvav
  • 2,808
  • 15
  • 31