zoukankan      html  css  js  c++  java
  • Stream Collector

         // 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));
  • 相关阅读:
    k8S 搭建集群
    K8S搭建
    华为交换机常用命令
    JSP中动态include和静态include的区别
    什么情况下调用doGet()和doPost()
    get和post的区别
    什么是spring框架
    spring的作用
    什么是DI
    依赖注入的三种实现方式
  • 原文地址:https://www.cnblogs.com/feika/p/4576808.html
Copyright © 2011-2022 走看看