• 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));
  • 相关阅读:
    系统运维易忘点总结之七
    SqlServer的排序规则
    Oracle数据库用户密码过期解决
    linux 查看即时网速 /流量的工具
    NFS部署过程
    NFS 共享存储
    Rsync服务的实战
    ncRsync服务
    架构名词
    状态码
  • 原文地址:https://www.cnblogs.com/jsersudo/p/11453918.html
Copyright © 2020-2023  润新知