函数式接口是实现 Lambda 表达式的前提
只包含一个抽象方法的接口
可通过 Lambda 表达式创建接口对象
在接口上使用 @FunctionInterface 注解,可以检查该接口是否是函数式接口
Java 常用四个函数式接口
| 函数式接口 | 参数类型 | 返回值 | 抽象方法 | 方法说明 |
|---|---|---|---|---|
| Consumer | T | void | void accept(T t) | 接收一个 T 类型的参数,对其进行操作 |
| Supplier | 无 | T | T get() | 返回 T 类型的对象 |
| Function | T | R | R apply(T t) | 接收 T 类型的参数,对其进行操作转换为 R 类型的对象,并返回 |
| Predicate | T | boolean | boolean test(T t) | 判断 T 类型的参数对象是否满足约束,并返回判断结果 |
四种函数式接口的基本使用
Consumer
Consumerconsumer = new Consumer () { @Override public void accept(String t) { System.out.println(t.toUpperCase()); } }; consumer.accept("hello");
Supplier
Suppliersupplier = new Supplier () { @Override public String get() { return "Hello world"; } }; System.out.println(supplier.get());;
Function
Functionfunction = new Function () { @Override public Integer apply(String str) { return Integer.parseInt(str); } }; System.out.println(function.apply("132"));
Predicate
Predicatepredicate = new Predicate () { @Override public boolean test(Integer t) { return t == 10; } }; System.out.println(predicate.test(5));;
如何自定义一个函数式接口
创建接口类
添加注解 @FunctionInterface
@FunctionalInterface public interface CaculateSum{ R sum(T t,U u); }
使用函数式接口
public static void main(String[] args) throws Exception {
CaculateSum caculateSum = new CaculateSum() {
@Override
public Integer sum(String t, Float u) {
return (int) (Integer.parseInt(t)+u);
}
};
System.out.println(caculateSum.sum("22", 10.2f));;
}
Lambda 是一个匿名函数,使用简洁更灵活
如何使用 Lambda 表达式
Lambda 表达式基本语法
(integer1,integer2)->Integer.compare(integer1, integer2)
-> :Lambda操作符(箭头操作符)
左边 : lambda形参列表,也就是函数接口中抽象方法的参数列表
右边 :接口中抽象方法的方法体
Lambda 基本使用
无参数,无返回值
new Thread(() -> {System.out.println("hello")}).start();
操作符右侧只有一条语句时,可以省略大括号
new Thread(() -> System.out.println("hello")).start();
需要一个参数,没有返回值
Consumer
只有一个参数时,小括号可以省略
Consumer
无参数,有返回值
Supplier
右侧只有一行表达式时,大括号和 return 可以省略
Supplier
需要一个参数,有返回值
Predicate
需要两个参数,有返回值
Comparatorcomparator = (x, y) -> Integer.compare(x, y);
Lambda 表达式作为函数式接口的实例,通常匿名内部类都可以使用 Lambda 实现
Lambda 表达式的另外一种写法:
//Consumer
方法引用格式
类(对象)::方法名
对象::非静态方法类::静态方法类::非静态方法
使用方法引用的前提
接口中的抽象方法参数列表和返回值类型与方法引用的方法(要调用的类的方法)的参数列表和返回值类型相同接口中的抽象方法的第一个参数可以调用引用方法,接口中的抽象方法的第二个参数是引用方法的参数
对象::非静态方法
Consumerconsumer = System.out::println; consumer.accept("对象::非静态方法");
类::静态方法
BiFunctionfunction = Integer::compare; System.out.println(function.apply(10, 20));; Comparator comparator = Integer::compare; System.out.println(comparator.compare(10, 20));;
类::非静态方法
Comparatorcomparator = Integer::compareTo; System.out.println(comparator.compare(10, 20));;
使用格式要求
方法引用:类名::new数组引用:数组类型 [ ]::new
使用前提
接口中的抽象方法参数列表需要与构造函数形参列表一致,返回值类型相同
实例说明
类名::new
Function 接口 中的抽象方法接收参数为String类型,返回值为Person类型,Person::new 的构造方法使用一个参数的构造方法,构造方法的返回值为当前类对象
public class App {
public static void main(String[] args) throws Exception {
Function function = Person::new;
System.out.println(function.apply("zhang").getName());
BiFunction biFunction = Person::new;
System.out.println(biFunction.apply("wang",20).getName());;
}
}
class Person{
private String name;
private int age;
public Person(String name, int age) { this.name = name;this.age = age;}
public Person(String name) { this.name = name;}
public Person() {}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getAge() { return age;}
public void setAge(int age) {this.age = age;}
}
数组类型 [ ]::new
Functionfunction = String[]::new; System.out.println(Arrays.toString(function.apply(5)));
Optional
Optional 如何创建 Optional 创建非 null 的对象 Optional 创建空的对象 Optional 创建可为 null 的对象 Optional 常用方法介绍 isPresent() :如果值存在则方法会返回 true,否则返回 falseisEmpty() : 如果不存在值则返回 true,否则返回 falseT orElse(T other) :如果存在该值,返回值, 否则返回 otherT get() : 返回 Optional 中的值,否则抛出异常:NoSuchElementExceptionT orElseGet(Supplier extends T> other) : 如果存在该值,返回值, 否则返回通过 Supplier接口创建的对象 public static
public static
public static



