JAVA8学习笔记-function
来源:互联网 发布:mysql并发insert死锁 编辑:程序博客网 时间:2024/05/29 19:00
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+UbiConsumer2 receive-->T+U------show consumer------consumer receive-->Tconsumer2 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+WthiConsumer2 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>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>
演示代码
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"; } }}
输出结果
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的差别在于提供的参数和返回结果
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 下载链接
0 0
- JAVA8学习笔记-function
- JAVA8学习笔记-Function
- Java8学习笔记(四)-Function接口详解
- java8 Stream学习笔记
- java8 stream学习笔记
- ConcurrentHashMap学习笔记(Java8)
- java8 注解学习笔记
- Java8学习笔记
- Java8 学习笔记之一
- Java8学习笔记
- Java8学习笔记
- JAVA8新特性学习笔记
- Java8 Stream类学习笔记
- Java8 Streams API 学习笔记
- Java8 学习笔记-stream-collection
- Java8官方文档学习笔记
- Java8 Stream API学习笔记
- Java8学习笔记 — Optional
- Android实现指定区域截屏的实现
- 欢迎使用CSDN-markdown编辑器
- python学习记录
- GridView & 自定义适配器
- oracle环境配置以及sys权限受限问题
- JAVA8学习笔记-function
- centos6.x安装mycat(简单的安装)
- LA 7456 Least Crucial Node
- 19-小学生考试系统
- Spring通过factory配置bean
- 使用js替换符号
- Android之lrucache原理以及获取系统应用和启动系统应用
- CWnd类总结
- 短信验证码按钮60秒倒计时