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> 
1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.