在java中使用groupby()进行动态分组

在java中使用groupby()进行动态分组,java,collections,group-by,java-stream,collectors,Java,Collections,Group By,Java Stream,Collectors,我正在处理一些场景 其中,用户选择一些列,并对它们进行分组并显示 我已经使用了criteria,现在我想使用javagroupby()进行分组 假设我有一名实体员工: package com.demo; public class Employee { int id; String name; String address; String phoneno; String designation; public Empl

我正在处理一些场景

其中,用户选择一些列,并对它们进行分组并显示

我已经使用了criteria,现在我想使用javagroupby()进行分组

假设我有一名实体员工:

package com.demo;

public class Employee {
    
    int id;
    String name;
    String address;
    String phoneno;
    String designation;
    
    
    public Employee(int id, String name, String address, String phoneno, String designation) {
        super();
        this.id = id;
        this.name = name;
        this.address = address;
        this.phoneno = phoneno;
        this.designation = designation;
    }
}

// getter and setters
我创建了另一个用于分组的类

public class GroupHere {
    public static void main(String arg[]) {

Function<Employee, List<Object>> classifier = (emp) ->
        Arrays.<Object>asList(emp.getAddress(),emp.getName());

Map<Employee, Long> groupedEmployee = employee.stream()
                  .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

System.out.println("key" + groupedEmployee .keySet());
System.out.println("Grouped by" + groupedEmployee );

   }
}
        
如何使用Employee的getter映射属性并添加到分类器中


或者他们是否有其他方法来对这些属性进行分组?

您可以使用从属性名称到属性提取器的映射来进行分组,即:

Map<String, Function<Employee, Object>> extractors = new HashMap<>();
extractors.put("name", Employee::getName);
extractors.put("address", Employee::getAddress);
extractors.put("phoneno", Employee::getPhoneno);
extractors.put("designation", Employee::getDesignation);
Map提取器=newhashmap();
提取器。put(“name”,Employee::getName);
提取器。put(“地址”,Employee::getAddress);
提取器。put(“phoneno”,Employee::getPhoneno);
提取器。放置(“指定”,员工::获取指定);
拥有此映射后,可以使用它动态创建分类器:

List<String> attributes = ...; // get attributes from JSON (left as exercise)

Function<Employee, List<Object>> classifier = emp -> attributes.stream()
    .map(attr -> extractors.get(attr).apply(emp))
    .collect(Collectors.toList());
列表属性=…;//从JSON获取属性(左为练习)
函数分类器=emp->attributes.stream()
.map(attr->extractors.get(attr.apply(emp))
.collect(Collectors.toList());
现在,您可以使用此分类器对员工进行GROUP:

Map<List<Object>, Long> groupedEmployees = employees.stream()
    .collect(Collectors.groupingBy(classifier, Collectors.counting()));
Map groupedEmployees=employees.stream()
.collect(收集器.groupingBy(分类器,收集器.counting());

您可以通过使用从属性名称到属性提取器的映射来实现,即:

Map<String, Function<Employee, Object>> extractors = new HashMap<>();
extractors.put("name", Employee::getName);
extractors.put("address", Employee::getAddress);
extractors.put("phoneno", Employee::getPhoneno);
extractors.put("designation", Employee::getDesignation);
Map提取器=newhashmap();
提取器。put(“name”,Employee::getName);
提取器。put(“地址”,Employee::getAddress);
提取器。put(“phoneno”,Employee::getPhoneno);
提取器。放置(“指定”,员工::获取指定);
拥有此映射后,可以使用它动态创建分类器:

List<String> attributes = ...; // get attributes from JSON (left as exercise)

Function<Employee, List<Object>> classifier = emp -> attributes.stream()
    .map(attr -> extractors.get(attr).apply(emp))
    .collect(Collectors.toList());
列表属性=…;//从JSON获取属性(左为练习)
函数分类器=emp->attributes.stream()
.map(attr->extractors.get(attr.apply(emp))
.collect(Collectors.toList());
现在,您可以使用此分类器对员工进行GROUP:

Map<List<Object>, Long> groupedEmployees = employees.stream()
    .collect(Collectors.groupingBy(classifier, Collectors.counting()));
Map groupedEmployees=employees.stream()
.collect(收集器.groupingBy(分类器,收集器.counting());
看一看看