Home >Java >javaTutorial >Function usage in java
Function interface is a functional interface used to represent a function that receives an input parameter and returns an output parameter. The specific usage is as follows: Create a Function instance: Use a lambda expression or method reference to create a Function. Call the apply method: Use the apply method to call Function, pass in input parameters and get the output result. The advantages of the Function interface include simplicity, reusability, composability, and parallelizability.
Function interface in Java
What is the Function interface?
Function interface is a functional interface used to represent a function that receives an input parameter and returns an output parameter.
Structure
<code class="java">@FunctionalInterface public interface Function<T, R> { R apply(T t); }</code>
Usage
The usage of Function interface is very simple:
Example
The following code creates a Function instance, converts the string to uppercase and returns:
<code class="java">Function<String, String> toUpperCase = s -> s.toUpperCase(); String result = toUpperCase.apply("hello"); // result 为 "HELLO"</code>
Advantages
Using the Function interface has the following advantages:
The above is the detailed content of Function usage in java. For more information, please follow other related articles on the PHP Chinese website!