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.
- 入参Function对每一个元素执行 如:.map(item ->new SelectModel(item.getName(), item.getValue()))转成一个新的对象
- 返回结果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.
- 入参Function对每一个元素执行 需要返回一个Long类型 如:.map(Student::getPower)
- 返回结果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.
- 入参为一个Collector 如:Collectors.groupingBy(item::getKey) 对list进行分组,groupingBy方法还可以传入第二个参数进行求和 求平均值计算 Collectors.groupingBy(p -> p.getYear() + "-" + p.getMonth(), Collectors.summarizingLong(SandboxiePowerCollect::getUsedPower))
- 或者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