《Java 1.8》函数式接口
一、介绍
1、函数式接口就是一个有且仅有一个抽象方法,但是可以有多个抽象方法的接口;
2、函数式接口可以被隐式转换为lambda表达式;
3、函数式接口可以现有的函数友好的支持lambda表达式;
二、函数式接口
| 序号 | 接口 | 描述 |
| 1 | BiConsumer<T,U> | 代表了一个接受两个输入参数的操作,并且不返回任何结果 |
| 2 | BiFunction<T,U,R> | 代表了一个接受两个输入参数的方法,并且返回一个结果 |
| 3 | BinaryOperator<T> | 代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果 |
| 4 | BiPredicate<T,U> | 代表了一个两个参数的boolean值方法 |
| 5 | BooleanSupplier | 代表了boolean值结果的提供方 |
| 6 | Consumer<T> | 代表了接受一个输入参数并且无返回的操作 |
| 7 | DoubleBinaryOperator | 代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。 |
| 8 | DoubleConsumer | 代表一个接受double值参数的操作,并且不返回结果。 |
| 9 | DoubleFunction<R> | 代表接受一个double值参数的方法,并且返回结果 |
| 11 | DoublePredicate | 代表一个拥有double值参数的boolean值方法 |
| 12 | DoubleSupplier | 代表一个double值结构的提供方 |
| 13 | DoubleToIntFunction | 接受一个double类型输入,返回一个int类型结果。 |
| 14 | DoubleToLongFunction | 接受一个double类型输入,返回一个long类型结果 |
| 15 | DoubleUnaryOperator | 接受一个参数同为类型double,返回值类型也为double 。 |
| 16 | Function<T,R> | 接受一个输入参数,返回一个结果。 |
| 17 | IntBinaryOperator | 接受两个参数同为类型int,返回值类型也为int 。 |
| 18 | IntConsumer | 接受一个int类型的输入参数,无返回值 。 |
| 19 | IntFunction<R> | 接受一个int类型输入参数,返回一个结果 |
| 20 | IntPredicate | 接受一个int输入参数,返回一个布尔值的结果。 |
| 21 | IntSupplier | 无参数,返回一个int类型结果。 |
| 22 | IntToDoubleFunction | 接受一个int类型输入,返回一个double类型结果 。 |
| 23 | IntToLongFunction | 接受一个int类型输入,返回一个long类型结果 |
| 24 | IntUnaryOperator | 接受一个参数同为类型int,返回值类型也为int 。 |
| 25 | LongBinaryOperator | 接受两个参数同为类型long,返回值类型也为long。 |
| 26 | LongConsumer | 接受一个long类型的输入参数,无返回值。 |
| 27 | LongFunction<R> | 接受一个long类型输入参数,返回一个结果。 |
| 28 | LongPredicate | R接受一个long输入参数,返回一个布尔值类型结果。 |
| 29 | LongSupplier | 无参数,返回一个结果long类型的值。 |
| 30 | LongToDoubleFunction | 接受一个long类型输入,返回一个double类型结果。 |
| 31 | LongToIntFunction | 接受一个long类型输入,返回一个int类型结果。 |
| 32 | LongUnaryOperator | 接受一个参数同为类型long,返回值类型也为long。 |
| 33 | ObjDoubleConsumer<T> | 接受一个object类型和一个double类型的输入参数,无返回值。 |
| 34 | ObjIntConsumer<T> | 接受一个object类型和一个int类型的输入参数,无返回值。 |
| 35 | ObjLongConsumer<T> | 接受一个object类型和一个long类型的输入参数,无返回值。 |
| 36 | Predicate<T> | 接受一个输入参数,返回一个布尔值结果。 |
| 37 | Supplier<T> | 无参数,返回一个结果。 |
| 38 | ToDoubleBiFunction<T,U> | 接受两个输入参数,返回一个double类型结果 |
| 39 | ToDoubleFunction<T> | 接受一个输入参数,返回一个double类型结果 |
| 40 | ToIntBiFunction<T,U> | 接受两个输入参数,返回一个int类型结果。 |
| 41 | ToIntFunction<T> | 接受一个输入参数,返回一个int类型结果。 |
| 42 | ToLongBiFunction<T,U> | 接受两个输入参数,返回一个long类型结果。 |
| 43 | ToLongFunction<T> | 接受一个输入参数,返回一个long类型结果。 |
| 44 | UnaryOperator<T> | 接受一个参数为类型T,返回值类型也为T。 |
三、调用示例
package com.learn.demo; import java.util.Comparator; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.BinaryOperator; import java.util.function.BooleanSupplier; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; public class FunctionDemo { /** * BiFunction 接收三个参数:通过Lambda表达式执行相关操作,主要方法apply,andThen两个方法 * apply方法:执行lambda表达式 * andThen方法:执行完lambda方法之后,需要搞得东西 * T - 表示第一个参数 * U - 表示第二个参数 * R - 表示返回结构result */ public static BiFunction<String, String, String> bifunction = (x,y) -> {return x+" ******* "+y;}; /** * BiConsumer 接收两个参数:没有返回值,主要方法accept */ public static BiConsumer<String,String> biConsumer = (x,y) -> {System.out.println(x+" ******* "+y);}; /** * BinaryOperator:泛型表示返回值类型,可以定义数学表达式,简单,可以复用 * 主要用途定义数学表达式 */ public static BinaryOperator<Integer> binaryOperator = (x,y) -> x+y; /** * BiPredicate:可以用来做逻辑判断 * 接收两个参数,返回boolean值 */ public static BiPredicate<Integer,Integer> biPredicate = (x,y) -> (x+y)>100; public static BiPredicate<Integer,Integer> biPredicate_and = (x,y) -> x > y; public static BiPredicate<Integer,Integer> biPredicate_or = (x,y) -> x > y; public static BiPredicate<Integer,Integer> biPredicate_negate = (x,y) -> x > y; /** * Function接收两个参数,返回一个结果 * T 输入参数 * R 输出结果 */ public static Function<String,String> function = x -> x + " Hello"; public static Function<String,String> function_compose = x -> x+" Word"; /** * 同BiPredicate */ public static Predicate<String> predicate = x -> x.equals("hello"); /** * 同BiPredicate */ public static void predicateDemo(){ System.out.println(predicate.test("hello")); } /** * compose方法会在apply之前执行 */ public static void functionDemo(){ String apply = function.apply("Java"); Function<String, String> compose = function.compose(function_compose); System.out.println(apply); System.out.println(compose.apply("Java")); } /** * Consumer: 不需要返回值,可以执行一个简单地动过 */ public static void consumerDemo(){ Consumer<String> consumer = x -> System.out.println(x); consumer.accept("abcd"); } /** * BooleanSupplier:根据lambda表达式返回boolean变量 */ public static void booleanSupplier(){ int x=3;int y=4; String abc1 = "abc"; String abc2 = "abc"; BooleanSupplier booleanSupplier1 = () -> x==y; BooleanSupplier booleanSupplier2 = () -> abc1.equals(abc2); System.out.println(booleanSupplier1.getAsBoolean()); System.out.println(booleanSupplier2.toString()); } /** * biPredicate:类似mysql后面拼接的where条件 * test:根据lambda判断是否满足条件 * and:表示是否同时满足 * negate:表示非运算,类似"!" * or:或者 */ public static void biPredicateDemo(){ System.out.println(biPredicate.test(12, 23)); System.out.println(biPredicate.and(biPredicate_and).test(77, 44)); System.out.println(biPredicate.or(biPredicate_or).test(77, 44)); System.out.println(biPredicate.negate().test(77, 44)); } /** * apply:一般用来设计算数表达式 * minBy:获取最小值 * maxBy:获取最大值 */ public static void binaryOperatorDemo(){ System.out.println(binaryOperator.apply(123, 234)); BinaryOperator<Integer> biMin = BinaryOperator.minBy(Comparator.naturalOrder()); BinaryOperator<Integer> biMax = BinaryOperator.maxBy(Comparator.naturalOrder()); System.out.println(biMin.apply(23, 32)); System.out.println(biMax.apply(23, 32)); } /** * 注意:biConsumer没有返回值 */ public static void biConsumerDemo(){ biConsumer.accept("beijing", "chaoyangqu"); } public static void biFunctionDemo(){ //apply方法测试 String apply = bifunction.apply("henan", "zhengzhou"); //and Then方法测试 Function<String,String> function = x -> x+" append"; BiFunction<String, String, String> thenfunction = bifunction.andThen(function); String andThen = thenfunction.apply("henan", "puyang"); System.out.println(apply); System.out.println(andThen); } public static void main(String[] args) { // biFunctionDemo(); // biConsumerDemo(); // binaryOperatorDemo(); // biPredicateDemo(); // consumerDemo(); // functionDemo(); predicateDemo(); } }

浙公网安备 33010602011771号