Built-in functional interface
There are at most 4 types of methods for the functional interface that may appear : there are return values with parameters, no return values with parameters, no return values with parameters, and judgment of true and false.
In order to simplify the definition of developers and unify the operation, Java provides a new development package: java.util.function, and provides the following four core functional interfaces in this package.
1. Functional Interface (Function)
The interface is defined as follows:
@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.
Example: Use a functional functional interface. This program will use the Function interface reference, String class "public boolean startsWith (String str)" method
package Project.Study.BuiltInFunctionalInterface;
import java.util.function.*;
public class Test1 {
public static void main(String args[]){
Function<String,Boolean>fun="Hello World"::startsWith;
//public boolean startsWith(String prefix, int toffset)方法
//startsWith() 方法用于检测字符串是否以指定的前缀开始。
System.out.println(fun.apply("H"));
System.out.println(fun.apply("e"));
System.out.println(fun.apply("ll"));
}
}
//结果:
//true
//false
//false
2. Consumer Interface (Consumer)
The interface is defined as follows:
@FunctionalInterface
public interface Consumer<T>{
public void accept(T t);
}
Main role: This interface is only responsible for receiving data (no need to return when referencing data), and does not return processing results.
Example: Use a consumer interface.
package Project.Study.BuiltInFunctionalInterface;
import java.util.function.Consumer;
public class Test2 {
public static void main(String args[]){
Consumer<String>cons=System.out::println;
cons.accept("Hello World");
}
}
//结果
//Hello World
This program uses the consumer interface to receive a reference to the System.out.println () method. This method definition needs to receive a String data, but it will not return any results.
3. Supply interface (Supplier)
The interface is defined as follows:
@FunctionalInterface
public interface Supplier<T>{
public T get();
}
Main function: This interface does not receive parameters, but can return results.
Example: supply interface. This program uses the toUpperCase () method of the String class (public String toUpperCase ()).
package Project.Study.BuiltInFunctionalInterface;
import java.util.function.Supplier;
public class Test3 {
public static void main(String args[]){
Supplier<String>sup="Hello World"::toUpperCase;
//toUpperCase的意思是将所有的英文字符转换为大写字母,只对英文字母有效,对除了A~Z和a~z的其余字符无任何效果
System.out.println(sup.get());,
}
}
//结果
//HELLO WORLD
4. Assertion interface (Predicate)
The interface is defined as follows:
@FunctionalInterface
public interface Predicate<T,R>{
public boolean test(T t);
}
The main role: to judge the use
Example: Assertive interface. This program will refer to the equalsIgnoreCase () method in the String class (public boolean equalsIgnoreCase (String str))
package Project.Study.BuiltInFunctionalInterface;
import java.util.function.Predicate;
public class Test4 {
public static void main(String args[]){
Predicate<String>pre="Hello World"::equalsIgnoreCase;
//执行忽略大小写的比较,当比较两个字符串时,它会认为A-Z和a-z是一样的
System.out.println(pre.test("HELLO WORLD"));
}
}
//结果:
//true