JAVA8学习笔记-function
2017-01-05 17:27
113 查看
JAVA8学习笔记-Function
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);
定制化自己的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.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";
}
}
}
输出结果
------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
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>
演示代码
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"; } } }
输出结果
biConsumer receive-->T+U biConsumer receive-->T+U function receive-->R W binaryOperator receive-->T+T T
Predicate
类和方法说明
相当于是Function的特殊形式 返回结果是boolean类型提供一定形式的boolean运算
BiPredicate<T,U> DoublePredicate IntPredicate LongPredicate Predicate<T>
Supplier
类和方法说明
不提供参数,获取一种类型的返回结果BooleanSupplier DoubleSupplier IntSupplier LongSupplier ##获取一个T类型的实例 Supplier<T> 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 下载链接相关文章推荐
- JAVA8学习笔记-Function
- java个人学习笔记04(function、overload、array、栈、堆)
- 学习笔记5:java 1.8 Predicate,Function,Supplier,Consumer,Comparator,Optional,Stream接口的使用
- Java中文处理学习笔记——Hello Unicode
- 我的Thinking in Java学习笔记(四) (zt)
- 我的Thinking in Java学习笔记(1)
- 我的Thinking in Java学习笔记(2)
- java对象序列化学习笔记
- Java中文处理学习笔记
- Thinking in Java学习笔记(三)(zt)
- 我的Thinking in Java学习笔记(八)
- JAVA学习笔记之Collection
- 我的Thinking in Java学习笔记(九)
- JAVAGUIDE(学习笔记1)
- java对象序列化学习笔记(z)
- 我的Thinking in Java学习笔记(六)(zt)
- JAVA与模式 学习笔记(一) 统一的建模语言UML介绍(2)
- JAVA与模式 学习笔记(一) 统一的建模语言UML介绍(2)
- 我的Thinking in Java学习笔记(四)
- 我的Thinking in Java学习笔记(七)