最新java基础强化

最新java基础强化方法 返回类型 描述 示例 List 把流中的元素收集到 List List emps list stream collect Collectors toList Set 把流中的元素收集到 Set Set emps list stream collect Collectors toSet Collection 把流中的元素收集到创建的集合 Collection

大家好,我是讯享网,很高兴认识大家。



方法 返回类型 描述 示例 List 把流中的元素收集到 List List emps= list.stream().collect(Collectors.toList()); Set 把流中的元素收集到 Set Set emps= list.stream().collect(Collectors.toSet()); Collection 把流中的元素收集到创建的集合 Collection emps= list.stream().collect(Collectors.toCollection(ArrayList::new)); counting() Long 计算流中元素的个数 list.stream().collect(Collectors.counting()); summingInt(ToIntFunction<? super T> mapper) Integer 对流中元素的整数属性求和 list.stream().collect(Collectors.summingInt(Employee::getSalary)); averagingInt(ToIntFunction<? super T> mapper) Double 计算流中元素整数属性的平均值 list.stream().collect(Collectors.averagingInt(Employee::getSalary)); summarizingInt(ToIntFunction<? super T> mapper) IntSummaryStatistics 收集流中整数属性的统计值,如平均值 list.stream().collect(Collectors.summarizingInt(Employee::getSalary)); String 连接流中的每个字符串 list.stream().map(Employee::getName).collect(Collectors.joining()); minBy(Comparator<? super T> comparator) Optional 根据比较器选择最小值 list.stream().collect(Collectors.minBy(Comparator.comparingInt(Employee::getSalary))); maxBy(Comparator<? super T> comparator) Optional 根据比较器选择最大值 list.stream().collect(Collectors.maxBy(Comparator.comparingInt(Employee::getSalary))); reducing(BinaryOperator op) 归约产生的类型 从一个作为累加器的初始值开始,利用 BinaryOperator 与流中元素逐个结合,从而规约成单个值 list.stream().collect(Collectors.reducing(0, Employee::getSalary, Integer::sum)); collectingAndThen(Collector<T,A,R> downstream, 最新java基础强化 Function<R,RR> finisher) 转换函数返回的类型 包裹另一个收集器,对其结果转换函数 list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), List::size)); groupingBy(Function<? super T, ? extends K> classifier) Map<K, List > 根据某属性值对流分组,属性为 K,结果为 V list.stream().collect(Collectors.groupingBy(Employee::getDepartmentName)); partitioningBy(Predicate<? super T> predicate) Map<Boolean, List > 根据 true 或 false 进行分区 list.stream().collect(Collectors.partitioningBy(Employee::getStatus));
小讯
上一篇 2024-12-27 12:02
下一篇 2024-12-27 13:58

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/9486.html