前言
本篇适用于使用过Java8函数式接口或者学习函数式接口的朋友(脉络认知),Java8 真香👀
函数式接口分类及细分图
- 消费型:Consumer
- 供给型:Supplier
- 断言型:Predicate
- 函数型:Function
- 操作型:operator
函数式接口代码实现
代码地址:Java8FunctionalInterfaceAPI.java,内容介绍🐱🐉:
消费型接口Consumer:
消费型接口Consumer中抽象方法为:accept()
// 接收一个参数消费
Consumer<String> consumer = System.out::println;
consumer.accept("Consumer FunctionalInterface");
// 接收两个参数消费
BiConsumer<String, String> biConsumer = (a, b) -> System.out.println(a + b);
biConsumer.accept("bi", "Consumer");
// ObjxxxConsumer 其中xxx为int、long、double,代表accept函数第二个参数类型
ObjIntConsumer<Integer> objIntConsumer = (a, b) -> System.out.println(a + b);
objIntConsumer.accept(1, 2);
供给型接口Supplier:
供给型接口Supplier中抽象方法为:get()
// 返回一个Integer类型整数
Supplier<Integer> supplier = () -> 1;
assertEquals(1, (int)supplier.get());
// BooleanSupplier、DoubleSupplier、IntSupplier、LongSupplier 顾名思义不废话
BooleanSupplier booleanSupplier = () -> true;
assertEquals(true, booleanSupplier.getAsBoolean());
断言型接口Predicate:
断言型接口Predicate中抽象方法为test(),同时还有三个方法:and、or、 negate 分别对应与、或、非操作
// 接收一个参数断言,Predicate有四个方法:test、and、or、 negate 分别对应 判断、与、或、非
Predicate<String> predicate = (a) -> "".equals(a);
assertEquals(false, predicate.test("s"));
assertEquals(true, predicate.negate().test("s"));
// 接收两个参数断言
BiPredicate<String, String> biPredicate = (a, b) -> a.equals(b);
assertEquals(false, biPredicate.test("s", "b"));
// 限定接收一个为double的参数:XXXPredicate 其中xxx为:Double、Int、Long
DoublePredicate doublePredivate = (a) -> Double.valueOf(0.0d).equals(a);
doublePredivate.test(1);
assertEquals(false, doublePredivate.test(1));
函数型接口Function:
函数型接口Function中抽象方法为apply(),同时提供了默认方法compose()和andThen(),分别对应前置和后置处理
// 接收一个T类型参数返回一个Integer类型的结果
Function<String, Integer> function = Integer::parseInt;
assertEquals(12, (int)function.apply("12"));
// 下面列举的是对Function的细化,没什么好说的,顾名思义
// IntFunction<R>(IntToLongFunction、IntToDoubleFunction)、
// LongFunction<R>(LongToIntFunction、LongToDoubleFunction)、
// DoubleFunction<R>(DoubleToIntFunction、DoubleToLongFunction)、
// ToxxxFunction(ToIntFunction<T>、ToDoubleFunction<T>、ToLongFunction<T>)
// BiFunction<T, U, R> 接收两个参数 返回一个R类型的结果
BiFunction<Integer, Integer, String> biFunction = (a, b) -> String.valueOf(a + b);
assertEquals("3", biFunction.apply(1, 2));
// 下面列举的是对BiFunction的细化
// ToIntBiFunction<T, U>、ToDoubleBiFunction<T, U>、ToLongBiFunction<T, U>
操作型接口 Operator:
// 接收一个T类型的参数返回一个相同类型的结果,UnaryOperator有一个静态方法identity,返回操作符UnaryOperator对象本身
UnaryOperator<Integer> unaryoperator = x -> x + 1;
assertEquals(11, (int)unaryoperator.apply(10));
// 下面列举的是对UnaryOperator的细化
// IntUnaryOperator、DoubleUnaryOperator、LongUnaryOperator
// 接收两个相同类型的参数,转换成相同类型的结果输出,BinaryOperator和BiFunction不同的地方在于,提供了两个静态函数:minBy、maxBy用来返回带有比较器的BinaryOperator
BinaryOperator<Integer> binaryOperator = (a, b) -> a + b;
assertEquals(3, (int)binaryOperator.apply(1, 2));
BinaryOperator<Integer> maxBinaryOperator = BinaryOperator.maxBy((a, b) -> Integer.compare(a, b));
assertEquals(2, (int)maxBinaryOperator.apply(1, 2));
// 下面列举的是对BinaryOperator<T>的细化
// IntBinaryOperator、DoubleBinaryOperator、LongBinaryOperator
IntBinaryOperator intBinaryOperator = (a, b) -> a + b;
assertEquals(3, (int)intBinaryOperator.applyAsInt(1, 2));
自定义函数接口实现方式
- @FunctionalInterface 声明只是用来类型检查,如果接口声明符合函数式接口规范,不使用@FunctionalInterface也是ok的🙃
- 函数式接口规范:接口有且只有一个抽象方法,可以有多个静态方法和多个默认方法
结尾
本篇内容为Java8函数式接口内容梳理,更细节的暂不讨论,觉得海星帮忙点个赞吧,你的认同就是博主继续写下去最大的动力。✨