Consumer接口主要是处理参数不提供返回结果的函数式接口
java自带的api提供的实现接口
//接受2种类型的参数,进行处理
BiConsumer
void accept(T t, U u);
default BiConsumer andThen(BiConsumer super T, ? super U> after);
//接受单个参数,进行处理
Consumer
void accept(T t);
default Consumer 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
void accept(T t, double value);
//接受T和int类型的参数,进行处理
ObjIntConsumer
void accept(T t, int value);
//接受T和long类型的参数,进行处理
ObjLongConsumer
void accept(T t, long value);
@FunctionalInterface
static interface ThiConsumer{
void accept(T t, U u, W w);
default ThiConsumer 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.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
Consumer
DoubleConsumer
IntConsumer
LongConsumer
ObjDoubleConsumer
ObjIntConsumer
ObjLongConsumer
*/
public static void main(String[] args) throws Exception {
System.out.println("------show biConsumer------");
BiConsumer biConsumer = (T t, U u)->{System.out.println(String.format("biConsumer receive-->%s+%s", t,u));};
BiConsumer 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 consumer = (T t)->{System.out.println(String.format("consumer receive-->%s", t));};
Consumer 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 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 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 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 thiConsumer = (T t, U u, W w)->{System.out.println(String.format("thiConsumer receive-->%s+%s+%s", t,u, w));};
ThiConsumer 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{
void accept(T t, U u, W w);
default ThiConsumer 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";
}
}
}
------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
接受一定数量的参数 同时提供一个返回结果
//接受两种类型(T,U)的参数,返回R类型的结果
BiFunction<T,U,R>
R apply(T t, U u);
default BiFunction<T, U, V> andThen(Function super R, ? extends V> after);
//接受2个T类型的参数,返回T类型的结果
BinaryOperator<T>
//接受2个double类型的参数,返回double类型的结果
DoubleBinaryOperator
//接受double类型的参数,返回T类型的结果
DoubleFunction
//接受double类型的参数,返回int类型的结果
DoubleToIntFunction
//接受double类型的参数,返回long类型的结果
DoubleToLongFunction
//接受double类型的参数,返回double类型的结果
DoubleUnaryOperator
//接受T类型的参数,返回R类型的结果
Function<T,R>
//接受2个int类型的参数,返回int类型的结果
IntBinaryOperator
//接受2个int类型的参数,返回R类型的结果
IntFunction
IntToDoubleFunction
IntToLongFunction
IntUnaryOperator
LongBinaryOperator
LongFunction
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>
import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
public class FunctionDemo {
/**
* BiFunction
BinaryOperator
DoubleBinaryOperator
DoubleFunction
DoubleToIntFunction
DoubleToLongFunction
DoubleUnaryOperator
Function
IntBinaryOperator
IntFunction
IntToDoubleFunction
IntToLongFunction
IntUnaryOperator
LongBinaryOperator
LongFunction
LongToDoubleFunction
LongToIntFunction
LongUnaryOperator
ToDoubleBiFunction
ToDoubleFunction
ToIntBiFunction
ToIntFunction
ToLongBiFunction
ToLongFunction
UnaryOperator
* @param args
*/
public static void main(String[] args) {
BiFunction 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 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 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";
}
}
}
biConsumer receive-->T+U
biConsumer receive-->T+U
function receive-->R
W
binaryOperator receive-->T+T
T
相当于是Function的特殊形式 返回结果是boolean类型
提供一定形式的boolean运算
BiPredicate<T,U>
DoublePredicate
IntPredicate
LongPredicate
Predicate<T>
不提供参数,获取一种类型的返回结果
BooleanSupplier
DoubleSupplier
IntSupplier
LongSupplier
##获取一个T类型的实例
Supplier
T get();
从上面的方法列表可以看出,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 |
java8 api 下载链接