1 Overview
Functional interface refers to an interface that has only one abstract method, but can have multiple non-abstract methods. The purpose of using it is to provide better support for the use of Lambda expressions, further achieve the goal of functional programming, and improve development efficiency.
2 Features of functional interface
- There is only one abstract method, but there can be members, static and default methods
- Use the annotation @FunctionalInterface to check whether an interface is a functional interface (the number of abstract methods is one)
- If the interface is a functional interface, you can add @FunctionalInterface or not
- If an interface contains more than one abstract method, after adding @FunctionalInterface, an error will be reported
Callback function : Simply put, the parameter passed in a method (A) is also a method (B), and method B is called in method A.
3 Use Lambda to implement functional interfaces
(1) The abstract method of the interface has no parameters
First define a functional interface
/**
* 只有一个抽象方法,符合函数式接口的规定
*/
@FunctionalInterface
public interface FuntionInterface_ {
public void laugh();
}
achieve:
/**
* 1.8新写法实现接口
*/
public class FunctionalInterface_03 {
public static void main(String[] args) {
call (() -> System.out.println("Lambda表达式实现接口"))
}
public static void call(FuntionInterface_ func){
func.laugh();
}
}
(2) The abstract method of the interface has parameters
Define the interface:
@FunctionalInterface
interface MyFunctionInter2 {
void printMessage(String str);
}
achieve:
public class FunctionalInterface{
public static void main(String[] args) {
String str = "有参函数式接口调用";
// 使用 lambda 实现有参函数式接口调用
// str 是字符串,s 是参数,str 给到 s ,打印 s
call ( s -> System.out.println(s), str);
}
//定义调用接口的回调函数
public static void call(MyFunctionInter2 func,String str) {
func.printMessage(str);
}
}
4 JDK's own functional interface
(1) Supplier: Represents the supplier, has a return value, can get the data, and use the get() method to get the data.
public class JdkOwn_01 {
public static void main(String[] args) {
String result = getResult(new Supplier<String>() {
result = getResult( ()-> "哈哈" );
System.out.println(result);//哈哈
// 创建Supplier容器
// 此时不会调用构造方法创建对象
Supplier<JdkOwn_01> sup = JdkOwn_01::new;
// 创建对象并返回
JdkOwn_01 jo1 = sup.get();
JdkOwn_01 jo2 = sup.get();
System.out.println(jo1);
System.out.println(jo2);
}
//定义获取方法,传入供应商接口对象,由返回值
public static String getResult(Supplier<String> function) {
//固定写法,通过get() 获取内容
//具体获取什么需要自己实现
return function.get();
}
}
(2) Consumer: Represents the consumer interface, no return value is required, accept(T t): Used to perform consumer operations.
public class JdkOwn_02 {
public static void main(String[] args) {
String message = "我爱花钱";
//lambda 实现接口
//message是参数
//因为该消费接口是需要参数的,需要定义 x ,接受 message
//然后打印 x
consumeResult(x -> System.out.println(x), message);
}
//定义消费方法,传入消费接口对象和消费的内容
public static void consumeResult(Consumer<String> function, String message) {
function.accept(message);
}
}
(3) Function<T,R>: Receive a parameter and produce a result. T is the parameter, R is the result, and the R apply(T t) method produces the result.
public class JdkOwn_03 {
public static void main(String[] args) {
int num = convertType(x->Integer.parseInt(x), "123");
System.out.println(num);
}
/**
* 接受一个字符串,将其转换为数字,数字是返回值
* @param function 自定义接口
* @param str 字符串,参数
* @return 数字,返回值
*/
public static int convertType(Function<String, Integer> function, String str) {
int num = function.apply(str);
return num;
}
}
(4) Predicate: assertion interface, used to make some judgments, booean test(T): used to verify and compare operations.
public class JdkOwn_04 {
public static boolean call(Predicate<String> predicate, String message) {
return predicate.test(message);
}
public static void main(String[] args) {
String message = "ok";
boolean result = call ( x -> {
return x.equals("ok");}, message);
System.out.println(result);
}
}