1. Built in functional interface
During the operation of method reference, it can be found that no matter how the operation is carried out, there are only four types of possible functional interface methods: return value with parameters, return value with parameters, return value without parameters, and true or false judgment. In order to simplify the definition of developers and realize the unification of operations, JDK1.8 provides a new development package: java. util. function, and provides the following four core functional interfaces in this package:
(1) Function al interface
@FunctionalInterface public interface Function<T, R>( public R apply(T t); }
Main function: this interface needs to receive a parameter and return a processing result.
(2) Consumer interface
@FunctionalInterface public interface Consumer<T> { public void accept(T t); }
Main function: this interface is only responsible for receiving data (it does not need to be returned when referencing data) and does not return processing results.
(3) Supply interface
@FunctionalInterface public interface Supplier<T> { public T get(); }
-Main function: this interface does not receive parameters, but can return results.
(4) Predicat e interface
@FunctionalInterface public interface Predicate<T> { public boolean test(T t);
Main function: for judgment operation.
Tip: there are many other interfaces with similar functions in the java util. function package. The above four are core interfaces.
2. Example
(1) Functional interface
Use the functional interface to receive parameters and return a processing result. In this operation, the public Boolean startswith (String STR) method of String class will be referenced by Function.
import java.util.function.Function; public class TestDemo { public static void main(String[] args) { Function<String,Boolean> fun = "##workhah"::startsWith; System.out.println(fun.apply("##"); / / is equivalent to calling startsWith() using an object } } //Program execution result: true
If you want to use a functional interface, you must ensure that there is an input parameter and a return value. Because the mapping is the startsWith0 method of String class, you must pass in the parameter (String type) and return a judgment result (boolean type) when using this method.
(2) Consumer interface
The consumer interface mainly receives parameters but does not return data, so the System.out.print(String str) method is mapped this time.
import java.util.function.Consumer; public class TestDemo { public static void main(String[] args) { Consumer<String> cons = System.out::print; cons.accept("hello world"); } } //Program execution result: hello world
This program uses the consumer interface to receive the reference of the System.out.print() method. This method definition needs to receive a String data, but it will not return any results.
(3) Supply type interface
The supplied interface does not receive any parameters, so the toUpperCase() method of String class is referenced this time.
import java.util.function.Supplier; public class TestDemo { public static void main(String[] args) { Supplier<String> sup = "workhah"::toUpperCase; System.out.println(sup.get()); } } //Program execution result: WORKHAH
This program uses a supplied functional interface. There is no need to receive parameters on this interface, so the toUpperCase() method is directly referenced by the instantiated object of String class. After calling the ` ` get() ` ` method, the uppercase conversion operation can be realized.
(4) Assertive interface
The assertion interface is mainly used for judgment. It needs to receive a parameter and return a judgment result (boolean). This time, we will refer to the equalsIgnoreCase() method in the String class.
import java.util.function.Predicate; public class TestDemo { public static void main(String[] args) { Predicate<String> pre = "workhah":: equalsIgnoreCase; System.out.println(pre.test("WORKHAH")); } } //Program execution result: true
This program directly refers to the equalsIgnoreCase() ordinary method of String class by using the assertion interface, and then ignores the case comparison.