jdk1.8 Stream

list.stream().

  • <R> Stream<R> map(Function<? super T, ? extends R> mapper);

      方法描述:Returns a stream consisting of the results of applying the given * function to the elements of this stream.

  1. 入参Function对每一个元素执行 如:.map(item ->new SelectModel(item.getName(), item.getValue()))转成一个新的对象
  2. 返回结果stream可进一步操作 如:.collect(Collectors.toList())转成list
  • LongStream mapToLong(ToLongFunction<? super T> mapper);

      方法描述:Returns a {@code LongStream} consisting of the results of applying the given function to the elements of this stream.

  1. 入参Function对每一个元素执行 需要返回一个Long类型 如:.map(Student::getPower)
  2. 返回结果stream可进一步操作 如:.sum()求和
  • <R, A> R collect(Collector<? super T, A, R> collector);

       方法描述:reduction</a> operation on the elements of this stream using a * {@code Collector}. A {@code Collector} * encapsulates the functions used as arguments to * {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of * collection strategies and composition of collect operations such as * multiple-level grouping or partitioning.

  1. 入参为一个Collector 如:Collectors.groupingBy(item::getKey) 对list进行分组,groupingBy方法还可以传入第二个参数进行求和 求平均值计算  Collectors.groupingBy(p -> p.getYear() + "-" + p.getMonth(), Collectors.summarizingLong(SandboxiePowerCollect::getUsedPower))
  2. 或者Collectors.toMap(CommonUtils::getKey, t -> t, (supply1, supply2) -> { supply1.setPower(supply1.getPower()+supply1.getPower()); return supply1 ; }),

toMap方法几个参数:Collectors.toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)第一个参数为key ,第二个参数为value 第三个参数为key相同时执行的表达式如(key1, key2) ->key1

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值