赞
踩
想了解更多JUC的知识
——JUC并发编程合集在Java8支持Lambda
表达式以后,为了满足Lambda表达式的一些典型使用场景,JDK为我们提供了大量常用的函数式接口。它们主要在java.util.function
包中。
可以大致分为四种类型:
Function
:功能型函数接口
Predicate
:断言型接口
Suppier
:供给型接口
Consumer
:消费型接口
Function接口是对实例进行处理转换的接口,定义了一个名叫apply()
的抽象方法,传入一个泛型T对象,并返回一个泛型R对象
案例
public class FunctionTest {
public static void main(String[] args) {
//传入一个数字,返回平方
Function<Integer, Integer> function = (num) -> {
return num * num;
};
System.out.println(function.apply(10));//100
}
}
与Function相关的接口
接口名称 | 方法名称 | 方法前面 |
---|---|---|
Function | apply | (T) => R |
BiFunction | apply | (T, U) => R |
DoubleFunction | apply | (double) => R |
DoubleToIntFunction | applyAsInt | (double) => int |
DoubleToLongFunction | applyAsLong | (double) => long |
IntFunction | apply | (int) => R |
IntToDoubleFunction | applyAsDouble | (int) => double |
IntToLongFunction | applyAsLong | (int) => long |
LongFunction | apply | (long) => R |
LongToDoubleFunction | applyAsDouble | (long) => double |
LongToIntFunction | applyAsInt | (long) => int |
ToDoubleFunction | applyAsDouble | (T) => double |
ToDoubleBiFunction | applyAsDouble | (T, U) => double |
ToIntFunction | applyAsInt | (T) => int |
ToIntBiFunction | applyAsInt | (T, U) => int |
ToLongFunction | applyAsLong | (T) => long |
ToLongBiFunction | applyAsLong | (T, U) => long |
Predicate接口是判断是与否的接口,定义了一个名叫test
的抽象方法,传入一个泛型T对象,并返回一个boolean类型
案例
public class PredicateTest {
public static void main(String[] args) {
Predicate predicate = (Object str)->{
return "a".equals(str);
};
System.out.println(predicate.test("a"));//true
System.out.println(predicate.test("A"));//false
}
}
与Predicate相关的接口
接口名称 | 方法名称 | 方法签名 |
---|---|---|
Predicate | test | (T) => boolean |
BiPredicate | test | (T, U) => boolean |
DoublePredicate | test | (double) => boolean |
IntPredicate | test | (int) => boolean |
LongPredicate | test | (long) => boolean |
Supplier接口是对象实例的提供者,定义了一个名叫get
的抽象方法,它不用传入参数,并返回一个泛型T对象
案例
public class FunctionInterfaceTest {
public static void main(String[] args) {
Supplier<Car> supplier = ()->{
return new Car("奔驰",1000);
};
//用supplier的get方法创建car对象
Car car = supplier.get();
System.out.println(car.getName() + "的价钱为:" + car.getPrice());
}
}
//一个汽车类,有两个属性:车名和价钱
class Car{
private String name;
private Integer price;
public Car(String name, Integer price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
与Supplier相关的接口
接口名称 | 方法名称 | 方法签名 |
---|---|---|
Supplier | get | () => T |
BooleanSupplier | getAsBoolean | () => boolean |
DoubleSupplier | getAsDouble | () => double |
IntSupplier | getAsInt | () => int |
LongSupplier | getAsLong | () => long |
Consumer接口是一个类似消费者的接口,定义了一个名叫accept
的抽象方法,传入一个泛型T对象,没有返回值
案例
public class FunctionInterfaceTest {
public static void main(String[] args) {
Supplier<Car> supplier = ()->{
return new Car("奔驰",1000);
};
Consumer<Car> consumer = (Car car)->{
System.out.println(car.getName() + "的价钱为:" + car.getPrice());
};
consumer.accept(supplier.get());
}
}
//一个汽车类,有两个属性:车名和价钱
class Car{
private String name;
private Integer price;
public Car(String name, Integer price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
与Consumer相关的接口
接口名称 | 方法名称 | 方法签名 |
---|---|---|
Consumer | accept | (T) => void |
DoubleConsumer | accept | (double) => void |
IntConsumer | accept | (int) => void |
LongConsumer | accept | (long) => void |
ObjDoubleConsumer | accept | (T, double) => void |
ObjIntConsumer | accept | (T, int) => void |
ObjLongConsumer | accept | (T, long) => void |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。