英文:
Dynamic Grouping using groupby() in java
问题
我正在处理一些场景。
在这种情况下,用户选择了一些列,然后对它们进行分组并显示出来。
我已经使用条件完成了,并且现在我想使用 Java 的 groupby() 函数来进行分组。
考虑我有一个实体类 Employee:
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);
}
}
现在我想要动态创建我的分类器,所以当用户选择了“name”和“phoneno”时,分类器应该包含这些属性的 getter 方法。
我在这里感到困惑。
此外,当用户选择属性/列时,详细信息以 JSON 格式给出。
{
[ attributes: name, groupable: true ],
[ attributes: phoneno, groupable: true ]
}
我如何将 attributes 映射到 Employee 的 getters 中,并添加到 classifier 中呢?
或者是否有其他方法可以对这些属性进行分组?
英文:
I am working on some scenario.
where the user selects some columns and the grouping on them is done and displayed.
I have done using criteria and now I want to use java groupby() to group.
Consider I have an entity Employee:
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
I have created another class for grouping
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 );
}
}
Now I wanted to dynamically create my classifier so when the user selects '''name''' and '''phoneno'''
the classifier should have these getters in it.
I am confused here.
Also when user selects the attributes/column that details is in JSON.
{
[ attributes: name, groupable: true ],
[ attributes: phoneno, groupable: true ]
}
How can I map the attributes with the getters of Employee and add into classifier ?
Or is their any other way to group this attributes?
答案1
得分: 4
你可以通过使用从属性名称到属性提取器的映射来实现,即:
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);
一旦你有了这个映射,你可以使用它动态地创建分类器:
List<String> attributes = ...; // 从 JSON 中获取属性(留作练习)
Function<Employee, List<Object>> classifier = emp -> attributes.stream()
.map(attr -> extractors.get(attr).apply(emp))
.collect(Collectors.toList());
现在,你可以使用这个分类器来分组员工:
Map<List<Object>, Long> groupedEmployees = employees.stream()
.collect(Collectors.groupingBy(classifier, Collectors.counting()));
这样,你就可以使用这个分类器来分组员工了。
英文:
You could do it by using a map from attribute names to attribute extractors, i.e:
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);
Once you have this map, you can use it to create your classifier dynamically:
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());
Now, you can use this classifier to gruop the employees:
Map<List<Object>, Long> groupedEmployees = employees.stream()
.collect(Collectors.groupingBy(classifier, Collectors.counting()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论