zoukankan      html  css  js  c++  java
  • Java--java.util.stream.Collectors文档实例

    // java.util.stream.Collectors 类的主要作用就是辅助进行各类有用的 reduction 操作,例如转变输出为 Collection,把 Stream 元素进行归组。

    public
    final class Collectors extends Object Implementations of Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc. The following are examples of using the predefined collectors to perform common mutable reduction tasks: // Accumulate names into a List List<String> list = people.stream().map(Person::getName).collect(Collectors.toList()); // Accumulate names into a TreeSet Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new)); // Convert elements to strings and concatenate them, separated by commas String joined = things.stream() .map(Object::toString) .collect(Collectors.joining(", ")); // Compute sum of salaries of employee int total = employees.stream() .collect(Collectors.summingInt(Employee::getSalary))); // Group employees by department Map<Department, List<Employee>> byDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment)); // Compute sum of salaries by department Map<Department, Integer> totalByDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.summingInt(Employee::getSalary))); // Partition students into passing and failing Map<Boolean, List<Student>> passingFailing = students.stream() .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
  • 相关阅读:
    Linux初级知识_04 -- 目录结构与目录管理
    查找无限整数序列的第n位1,2,3,4,5,6,7,8,9,10,11,...
    谷歌面试题:在半径为1的圆中随机选取一点
    FtpClient 调用storeFile 抛出 java.net.SocketException异常
    CountDownLatch 使用
    软件版本号比较 java工具类
    pcm文件转wav C语言
    unimrcp更改安装目录
    xpath学习记录
    jackson 实体转json 为NULL或者为空不参加序列化
  • 原文地址:https://www.cnblogs.com/jsersudo/p/11453918.html
Copyright © 2011-2022 走看看