Search code examples
javafunctionlambdajava-11jshell

How to Call user-defined Lambda Function in Java


In this tutorial, I saw an example of a user-defined lambda function.

Function<String, String> toLowerCase = (var input) -> input.toLowerCase();

What I am wondering is how can I call this function? I tried it in jshell but am unable to. I can create the function fine :

Any ideas?

jshell> Function<String, String> toLowerCase = (var input) -> input.toLowerCase();
toLowerCase ==> $Lambda$16/0x00000008000b3040@3e6fa38a

but can't seem to execute it :

jshell> String hi = "UPPER";
jshell> String high;
high ==> null

jshell> toLowerCase(high,low);
|  Error:
|  cannot find symbol
|    symbol:   method toLowerCase(java.lang.String,java.lang.String)
|  toLowerCase(high,low);
|  ^---------^

jshell> 

Solution

  • You made an instance, type of a Function functional interface, which has a method Function.apply(). Therefore, you have to use it in the same way you'd use in a Java class:

    toLowerCase.apply(high);
    

    What in the toLowerCase(high,low); made you think the low is? Like in Java, you have to work with the defined methods and variables in the available scope.