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?
The lambda expressions are easy and contain three parts like parameters (method arguments), arrow operator (->) and expressions (method body). The lambda expressions can be categorized into three types: no parameter lambda expressions, single parameter lambda expressions and multiple parameters lambda expressions.
A lambda function can have any number of parameters, but the function body can only contain one expression. Moreover, a lambda is written in a single line of code and can also be invoked immediately.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
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);
Try :
BiFunction<Integer, Integer, String> lambda = (a, b) -> ("Given values are " + a + ", " + b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With