1

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> 
Naman
  • 27,789
  • 26
  • 218
  • 353
richie
  • 21
  • 2
  • `toLowerCase` is a `Function` https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html – Ryuzaki L Feb 26 '20 at 16:56

2 Answers2

4

You need to apply the Function such as:

toLowerCase.apply(high)

enter image description here

or assign its value to another variable low as in:

jshell> String low = toLowerCase.apply(high)
low ==> "ggggg"


Suggestion: Use jshell auto-completion (with tab key on macOS) to figure out what all methods are applicable to be called on the variables declared.

enter image description here

Naman
  • 27,789
  • 26
  • 218
  • 353
1

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.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183