java8笔记

java.util.function里提供了Predicate,Consumer和Function三个函数式接口

Predicate:T->boolean

Consumer:T->{}

Function:T->R

同时提供了多个针对原始类型操作的函数式接口

常用函数式接口
函数是接口函数描述符原始类型特化
Predicate<T>T->booleanIntPredicate,LongPredicate,DoublePredicate
Consumer<T>T->voidIntConsumer,LongConsumer,DoubleConsumer
Function<T,R>T->RIntFunction<R>,IntToDoubleFunction,IntToLongFunction
LongFunction<R>,LongToDoubleFunction,LongToIntFunction
DoubleFunction<R>,DoubleToIntFunction,DoubleToLongFunction
ToIntFunction<T>,ToLongFunction<T>,ToDoubleFunction<T>
Supplier<T>()->TBooleanSupplier,IntSupplier,LongSupplier,DoubleSupplier
UnaryOperator<T>T->TIntUnaryOperator,LongUnaryOperator,DoubleUnaryOperator
BinaryOperator<T>(T,T)->TIntBinaryOperator,LongBinaryOperator,DoubleBinaryOperator
BiPredicate<L,R>(L,R)->boolean 
BiConsumer<T,U>(T,U)->voidObjIntConsumer<T>,ObjLongConsumer<T>,ObjDoubleConsumer<T>
BiFunction<T,U,R>(T,U)->RToIntBiFunction<T,U>,ToLongBiFunction<T,U>,ToDoubleBiFunction<T,U>

比较器复合:

静态方法Comparator.comparing返回一个T->int, .reversed()反转排序,.thenComparing()二次排序

例如按苹果重量递减排序,重量一样得按国家排:

appleList.sort(comparing(Apple::getWeight)).reverse().thenComparing(Apple::getCountry)

谓词接口:

negate(非),or(或),and(与)

运算顺序看作与前面得结果运算,即a.or(b).and(c)=(a || b) && c

复合函数

andThen 和 compose  令Function<Integer,Integer> f = x -> x + 1;Function<Integer,Integer> g = x -> x * 2;

f.andThen(g):f的输出当作g的输入=g(f(x)) = (x+1)*2

f.compose(g): g的输出当作f的输入=f(g(x))=x*2+1


流只能forEach一次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值