Consumer
类和方法说明
Consumer接口主要是处理参数不提供返回结果的函数式接口
Java自带的api提供的实现接口
BiConsumer<T,U> void accept(T t, U u); default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after);Consumer<T> void accept(T t); default Consumer<T> andThen(Consumer<? super T> after);DoubleConsumer void accept(double value); default DoubleConsumer andThen(DoubleConsumer after);IntConsumer void accept(int value); default IntConsumer andThen(IntConsumer after);LongConsumer void accept(long value); default LongConsumer andThen(LongConsumer after);ObjDoubleConsumer<T> void accept(T t, double value);ObjIntConsumer<T> void accept(T t, int value);ObjLongConsumer<T> void accept(T t, long value);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
定制化自己的consumer–ThiConsumer
处理三个参数
@FunctionalInterface static interface ThiConsumer<T,U,W>{ void accept(T t, U u, W w); default ThiConsumer<T,U,W> andThen(ThiConsumer<? super T,? super U,? super W> consumer){ return (t, u, w)->{ accept(t, u, w); consumer.accept(t, u, w); }; } }
演示代码
import java.util.function.BiConsumerimport java.util.function.Consumerimport java.util.function.DoubleConsumerimport java.util.function.IntConsumerimport java.util.function.LongConsumerimport java.util.function.ObjDoubleConsumerimport java.util.function.ObjIntConsumerimport java.util.function.ObjLongConsumerpublic class ConsumerDemo { public static void main(String[] args) throws Exception { System.out.println("------show biConsumer------") BiConsumer<T, U> biConsumer = (T t, U u)->{System.out.println(String.format("biConsumer receive-->%s+%s", t,u)) BiConsumer<T, U> biConsumer2 = (T t, U u)->{System.out.println(String.format("biConsumer2 receive-->%s+%s", t,u)) biConsumer.andThen(biConsumer2).accept(new T(), new U()) System.out.println("------show consumer------") Consumer<T> consumer = (T t)->{System.out.println(String.format("consumer receive-->%s", t)) Consumer<T> consumer2 = (T t)->{System.out.println(String.format("consumer2 receive-->%s", t)) consumer.andThen(consumer2).accept(new T()) System.out.println("------show doubleConsumer------") DoubleConsumer doubleConsumer = (d)->{System.out.println(String.format("doubleConsumer receive-->%s", d)) doubleConsumer.accept(100_111.111_001d) System.out.println("------show intConsumer------") IntConsumer intConsumer = (i)->{System.out.println(String.format("doubleConsumer receive-->%s", i)) intConsumer.accept(1_111) System.out.println("------show longConsumer------") LongConsumer longConsumer = (l)->{System.out.println(String.format("longConsumer receive-->%s", l)) longConsumer.accept(111_111_111_111L) System.out.println("------show longConsumer------") ObjDoubleConsumer<T> objDoubleConsumer = (T t, double d)->{System.out.println(String.format("objDoubleConsumer receive-->%s+%s", t,d)) objDoubleConsumer.accept(new T(), 100_111.111_001d) System.out.println("------show objIntConsumer------") ObjIntConsumer<T> objIntConsumer = (T t, int i)->{System.out.println(String.format("objIntConsumer receive-->%s+%s", t,i)) objIntConsumer.accept(new T(), 1_111) System.out.println("------show objLongConsumer------") ObjLongConsumer<T> objLongConsumer = (T t, long l)->{System.out.println(String.format("objLongConsumer receive-->%s+%s", t,l)) objLongConsumer.accept(new T(), 111_111_111_111L) System.out.println("------show biConsumer------") ThiConsumer<T, U, W> thiConsumer = (T t, U u, W w)->{System.out.println(String.format("thiConsumer receive-->%s+%s+%s", t,u, w)) ThiConsumer<T, U, W> thiConsumer2 = (T t, U u, W w)->{System.out.println(String.format("thiConsumer2 receive-->%s+%s+%s", t,u, w)) thiConsumer.andThen(thiConsumer2).accept(new T(), new U(), new W()) } @FunctionalInterface static interface ThiConsumer<T,U,W>{ void accept(T t, U u, W w) default ThiConsumer<T,U,W> andThen(ThiConsumer<? super T,? super U,? super W> consumer){ return (t, u, w)->{ accept(t, u, w) consumer.accept(t, u, w) } } } static class T{ @Override public String toString() { return "T" } } static class U{ @Override public String toString() { return "U" } } static class W{ @Override public String toString() { return "W" } } static class R{ @Override public String toString() { return "R" } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
输出结果
------ ------ -->+ -->+------ ------ --> -->------ ------ -->.------ ------ -->------ ------ -->------ ------ -->+.------ ------ -->+------ ------ -->+------ ------ -->++ -->++
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Function
类和方法说明
接受一定数量的参数 同时提供一个返回结果
//接受两种类型(T,U)的参数,返回R类型的结果BiFunction<T,U,R> R apply(T t, U u); default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after);//接受2个T类型的参数,返回T类型的结果BinaryOperator<T>//接受2个double类型的参数,返回double类型的结果DoubleBinaryOperator//接受double类型的参数,返回T类型的结果DoubleFunction<R>//接受double类型的参数,返回int类型的结果DoubleToIntFunction//接受double类型的参数,返回long类型的结果DoubleToLongFunction//接受double类型的参数,返回double类型的结果DoubleUnaryOperator//接受T类型的参数,返回R类型的结果Function<T,R>//接受2个int类型的参数,返回int类型的结果IntBinaryOperator//接受2个int类型的参数,返回R类型的结果IntFunction<R>IntToDoubleFunctionIntToLongFunctionIntUnaryOperatorLongBinaryOperatorLongFunction<R>LongToDoubleFunctionLongToIntFunctionLongUnaryOperator//接受两种类型(T,U)的参数,返回double类型的结果ToDoubleBiFunction<T,U>//接受T类型的参数,返回double类型的结果ToDoubleFunction<T>ToIntBiFunction<T,U>ToIntFunction<T>ToLongBiFunction<T,U>ToLongFunction<T>//接受T类型的参数,返回T类型的结果UnaryOperator<T>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
演示代码
import java.util.Random;import java.util.function.BiFunction;import java.util.function.BinaryOperator;import java.util.function.Function;public class FunctionDemo {/** * BiFunction<T,U,R>BinaryOperator<T>DoubleBinaryOperatorDoubleFunction<R>DoubleToIntFunctionDoubleToLongFunctionDoubleUnaryOperatorFunction<T,R>IntBinaryOperatorIntFunction<R>IntToDoubleFunctionIntToLongFunctionIntUnaryOperatorLongBinaryOperatorLongFunction<R>LongToDoubleFunctionLongToIntFunctionLongUnaryOperatorToDoubleBiFunction<T,U>ToDoubleFunction<T>ToIntBiFunction<T,U>ToIntFunction<T>ToLongBiFunction<T,U>ToLongFunction<T>UnaryOperator<T> * @param args */ public static void main(String[] args) { BiFunction<T, U, R> biFunction = (T t, U u) -> { System.out.println(String .format("biConsumer receive-->%s+%s", t, u)); return new R(); }; biFunction.apply(new T(), new U()); Function<R, W> function = (R r)->{System.out.println(String .format("function receive-->%s", r)); return new W();}; W w = biFunction.andThen(function).apply(new T(), new U()); System.out.println(w); BinaryOperator<T> binaryOperator = (T t1, T t2)->{ System.out.println(String .format("binaryOperator receive-->%s+%s", t1, t2)); return new Random().nextInt(10)>=5?t2:t1;}; T tr = binaryOperator.apply(new T(), new T()); System.out.println(tr); } static class T{ @Override public String toString() { return "T"; } } static class U{ @Override public String toString() { return "U"; } } static class W{ @Override public String toString() { return "W"; } } static class R{ @Override public String toString() { return "R"; } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
输出结果
biConsumer receive-->T+UbiConsumer receive-->T+Ufunction receive-->RWbinaryOperator receive-->T+TT
Predicate
类和方法说明
相当于是Function的特殊形式 返回结果是boolean类型
提供一定形式的boolean运算
BiPredicate<T,U>DoublePredicateIntPredicateLongPredicatePredicate<T>
Supplier
类和方法说明
不提供参数,获取一种类型的返回结果
BooleanSupplierDoubleSupplierIntSupplierLongSupplier##获取一个T类型的实例Supplier<T> T get();
总结
从上面的方法列表可以看出,Consumer,Function,Predicate,Supplier的差别在于提供的参数和返回结果
| 无返回结果 | 返回boolean结果 | 返回其他类型结果 | 不提供参数()->{};(runable实现等)BooleanSupplierDoubleSupplier,IntSupplier,LongSupplier,Supplier提供一个参数Consumer
DoubleConsumer
IntConsumer
LongConsumerDoublePredicate
IntPredicate
LongPredicate
PredicateDoubleFunction
DoubleToIntFunction
DoubleToLongFunction
DoubleUnaryOperator
Function
IntFunction
IntToDoubleFunction
IntToLongFunction
IntUnaryOperator
LongFunction
LongToDoubleFunction
LongToIntFunction
LongUnaryOperator
ToDoubleFunction
ToIntFunction
ToLongFunction
UnaryOperator提供两个参数BiConsumer
ObjDoubleConsumer
ObjIntConsumer
ObjLongConsumerBiPredicateBiFunction
BinaryOperator
DoubleBinaryOperator
IntBinaryOperator
LongBinaryOperator
ToDoubleBiFunction
ToIntBiFunction
ToLongBiFunction附件
java8 api 下载链接