前言:
而今同学们对“java数据分组算法”都比较注意,看官们都需要学习一些“java数据分组算法”的相关内容。那么小编同时在网上搜集了一些有关“java数据分组算法””的相关知识,希望咱们能喜欢,咱们一起来了解一下吧!Collectors类中的groupingBy()方法返回一个收集器,用于在将数据收集到Map之前对数据进行分组。 它类似于SQL中的“group by”子句。
groupingBy()方法是重载的,它有三个版本:
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)
在前两个版本中,收集器负责为您创建Map对象。第三个版本允许我们指定一个用作工厂的Supplier来获取Map对象。
classifier 函数生成地图中的键。 collector 对与每个键相关联的值执行简化操作。
以下代码显示了如何按性别和计算每个组中的人数。
Collectors类的counting()方法返回要计数的Collector流中的元素数。
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; }}
上面的代码生成以下结果。
例2
以下代码显示如何按性别对person的名称进行分组:
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; }}
上面的代码生成以下结果。
例3
以下代码显示了如何在列表中收集员工姓名。
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; }}
上面的代码生成以下结果。
例4
以下代码按性别分组人员。在每个性别组中,它创建另一个组他们出生的月份,并列出了在这个组出生的人的名字。
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; }}
上面的代码生成以下结果。
例5
以下代码显示如何按性别分组汇总收入统计。
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; }}
上面的代码生成以下结果。
标签: #java数据分组算法