转载于:http://blog.csdn.net/weixin_29135773/article/details/54095573
Consumer
类和方法说明
Consumer接口主要是处理参数不提供返回结果的函数式接口
java自带的api提供的实现接口
//接受2种类型的参数,进行处理 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); //接受double类型的参数,进行处理 DoubleConsumer void accept(double value); default DoubleConsumer andThen(DoubleConsumer after); //接受int类型的参数,进行处理 IntConsumer void accept(int value); default IntConsumer andThen(IntConsumer after); //接受long类型的参数,进行处理 LongConsumer void accept(long value); default LongConsumer andThen(LongConsumer after); //接受T和double类型的参数,进行处理 ObjDoubleConsumer<T> void accept(T t, double value); //接受T和int类型的参数,进行处理 ObjIntConsumer<T> void accept(T t, int value); //接受T和long类型的参数,进行处理 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
定制化自己的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);
};
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
演示代码
import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.DoubleConsumer; import java.util.function.IntConsumer; import java.util.function.LongConsumer; import java.util.function.ObjDoubleConsumer; import java.util.function.ObjIntConsumer; import java.util.function.ObjLongConsumer; public class ConsumerDemo { /*BiConsumer<T,U>
Consumer<T>
DoubleConsumer
IntConsumer
LongConsumer
ObjDoubleConsumer<T>
ObjIntConsumer<T>
ObjLongConsumer<T>
*/ 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
输出结果
------show biConsumer------ biConsumer receive-->T+U biConsumer2 receive-->T+U ------show consumer------ consumer receive-->T consumer2 receive-->T ------show doubleConsumer------ doubleConsumer receive-->100111.111001 ------show intConsumer------ doubleConsumer receive-->1111 ------show longConsumer------ longConsumer receive-->111111111111 ------show longConsumer------ objDoubleConsumer receive-->T+100111.111001 ------show objIntConsumer------ objIntConsumer receive-->T+1111 ------show objLongConsumer------ objLongConsumer receive-->T+111111111111 ------show biConsumer------ thiConsumer receive-->T+U+W thiConsumer2 receive-->T+U+W
- 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>
IntToDoubleFunction
IntToLongFunction
IntUnaryOperator
LongBinaryOperator
LongFunction<R>
LongToDoubleFunction
LongToIntFunction
LongUnaryOperator
//接受两种类型(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
演示代码
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>
DoubleBinaryOperator
DoubleFunction<R>
DoubleToIntFunction
DoubleToLongFunction
DoubleUnaryOperator
Function<T,R>
IntBinaryOperator
IntFunction<R>
IntToDoubleFunction
IntToLongFunction
IntUnaryOperator
LongBinaryOperator
LongFunction<R>
LongToDoubleFunction
LongToIntFunction
LongUnaryOperator
ToDoubleBiFunction<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
输出结果
biConsumer receive-->T+U
biConsumer receive-->T+U function receive-->R
W
binaryOperator receive-->T+T T
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Predicate
类和方法说明
相当于是Function的特殊形式 返回结果是boolean类型
提供一定形式的boolean运算
BiPredicate<T,U> DoublePredicate
IntPredicate
LongPredicate
Predicate<T>
- 1
- 2
- 3
- 4
- 5
Supplier
类和方法说明
不提供参数,获取一种类型的返回结果
BooleanSupplier
DoubleSupplier
IntSupplier
LongSupplier ##获取一个T类型的实例 Supplier<T> T get();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
总结
从上面的方法列表可以看出,Consumer,Function,Predicate,Supplier的差别在于提供的参数和返回结果
无返回结果 | 返回boolean结果 | 返回其他类型结果 | |
---|---|---|---|
不提供参数 | ()->{};(runable实现等) | BooleanSupplier | DoubleSupplier,IntSupplier,LongSupplier,Supplier |
提供一个参数 |
Consumer DoubleConsumer IntConsumer LongConsumer |
DoublePredicate IntPredicate LongPredicate Predicate |
DoubleFunction DoubleToIntFunction DoubleToLongFunction DoubleUnaryOperator Function IntFunction IntToDoubleFunction IntToLongFunction IntUnaryOperator LongFunction LongToDoubleFunction LongToIntFunction LongUnaryOperator ToDoubleFunction ToIntFunction ToLongFunction UnaryOperator |
提供两个参数 |
BiConsumer ObjDoubleConsumer ObjIntConsumer ObjLongConsumer |
BiPredicate |
BiFunction BinaryOperator DoubleBinaryOperator IntBinaryOperator LongBinaryOperator ToDoubleBiFunction ToIntBiFunction ToLongBiFunction |