Collectors類中的groupingBy()方法返回一個(gè)收集器,用于在將數(shù)據(jù)收集到Map之前對(duì)數(shù)據(jù)進(jìn)行分組。 它類似于SQL中的“group by”子句。
groupingBy()方法是重載的,它有三個(gè)版本:
groupingBy(Function<? super T,? extends K> classifier) groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream) groupingBy(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
在前兩個(gè)版本中,收集器負(fù)責(zé)為您創(chuàng)建Map對(duì)象。第三個(gè)版本允許我們指定一個(gè)用作工廠的Supplier來獲取Map對(duì)象。
classifier
函數(shù)生成地圖中的鍵。 collector
對(duì)與每個(gè)鍵相關(guān)聯(lián)的值執(zhí)行簡化操作。
以下代碼顯示了如何按性別和計(jì)算每個(gè)組中的人數(shù)。
Collectors類的counting()方法返回要計(jì)數(shù)的Collector流中的元素?cái)?shù)。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender, Long> countByGender = Employee.persons() .stream() .collect(Collectors.groupingBy(Employee::getGender, Collectors.counting())); System.out.println(countByGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public Gender getGender() { return gender; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
以下代碼顯示如何按性別對(duì)person的名稱進(jìn)行分組:
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender, String> namesByGender = Employee.persons() .stream() .collect(Collectors.groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.joining(", ")))); System.out.println(namesByGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public String getName() { return name; } public Gender getGender() { return gender; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
以下代碼顯示了如何在列表中收集員工姓名。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender, List<String>> namesByGender = Employee.persons() .stream() .collect(Collectors.groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.toList()))); System.out.println(namesByGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public String getName() { return name; } public Gender getGender() { return gender; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
以下代碼按性別分組人員。在每個(gè)性別組中,它創(chuàng)建另一個(gè)組他們出生的月份,并列出了在這個(gè)組出生的人的名字。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map personsByGenderAndDobMonth = Employee.persons() .stream() .collect(Collectors.groupingBy(Employee::getGender, Collectors.groupingBy(p -> p.getDob().getMonth(), Collectors.mapping(Employee::getName,Collectors.joining(", "))))); System.out.println(personsByGenderAndDobMonth); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public String getName() { return name; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public LocalDate getDob() { return dob; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
以下代碼顯示如何按性別分組匯總收入統(tǒng)計(jì)。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender, DoubleSummaryStatistics> incomeStatsByGender = Employee.persons() .stream() .collect(Collectors.groupingBy(Employee::getGender, Collectors.summarizingDouble(Employee::getIncome))); System.out.println(incomeStatsByGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public String getName() { return name; } public Gender getGender() { return gender; } public double getIncome() { return income; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
更多建議: