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));
  • 相关阅读:
    Pure播放器
    WPF绑定并转换
    WPF的DataTrigger使用
    NancyFx框架之检测任务管理器
    Asp.Net MVC 5使用Identity之简单的注册和登陆
    AspNetCore使用MySQL
    Head First 设计模式之适配器模式与外观模式
    Head First 设计模式之命令模式(CommandPattern)
    Head First 设计模式之工厂模式(Factory Pattern)
    .NET设计规范————类型设计规范
  • 原文地址:https://www.cnblogs.com/jsersudo/p/11453918.html
Copyright © 2011-2022 走看看