java 8 复合Lambda 表达式

comparator  比较器复合

//排序

Comparator.comparing(Apple::getWeight);
List<Apple> list = Stream.of(new Apple(1, "a"), new Apple(2, "b"), new Apple(3, "c"))
        .collect(Collectors.toList());

list.sort(Comparator.comparing(Apple::getWeight));  //按重量排序

list.stream()
        .sorted(Comparator.comparing(Apple::getName))
        .collect(Collectors.toList());

//先按重量倒序,如果重量一样,再按名称排序
list.sort(Comparator.comparing(Apple::getWeight)
        .reversed()
        .thenComparing(Apple::getName));

 

Predicate 谓词 复合

//判断是苹果b
Predicate<Apple> isBApple = apple -> Objects.equals("b", apple.getName());

isBApple.negate();      //不是苹果b


//不是苹果b,并且苹果重量大于1  -->  apple.name != "b" && apple.weight > 1
isBApple.negate().and(apple -> apple.getWeight() > 1);

// ((apple.name != "b") && apple.weight > 1)  or apple.weight==0
isBApple.negate()
        .and(apple -> apple.getWeight() > 1)
        .or(apple -> apple.getWeight()==0);

Function 函数复合

//f(x) = x+1
Function<Integer, Integer> f = x -> x + 1;
//g(x) = x*2
Function<Integer, Integer> g = x -> x * 2;
// g(f(x))
Function<Integer, Integer> h = f.andThen(g);
// f(g(x))
Function<Integer, Integer> z = f.compose(g);

 

 

 

 

 

转载于:https://my.oschina.net/u/2552286/blog/1860403

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值