Module java.base

Class Collectors


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 =

 // Accumulate names into a TreeSet
 Set<String> set =

 // Convert elements to strings and concatenate them, separated by commas
 String joined =
   .collect(Collectors.joining(", "));

 // Compute sum of salaries of employee
 int total =

 // Group employees by department
 Map<Department, List<Employee>> byDept =

 // Compute sum of salaries by department
 Map<Department, Integer> totalByDept =

 // Partition students into passing and failing
 Map<Boolean, List<Student>> passingFailing =
   .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));