动态分组使用 groupby() 在 Java 中。

huangapple go评论162阅读模式
英文:

Dynamic Grouping using groupby() in java

问题

我正在处理一些场景。

在这种情况下,用户选择了一些列,然后对它们进行分组并显示出来。

我已经使用条件完成了,并且现在我想使用 Java 的 groupby() 函数来进行分组。

考虑我有一个实体类 Employee:

  1. package com.demo;
  2. public class Employee {
  3. int id;
  4. String name;
  5. String address;
  6. String phoneno;
  7. String designation;
  8. public Employee(int id, String name, String address, String phoneno, String designation) {
  9. super();
  10. this.id = id;
  11. this.name = name;
  12. this.address = address;
  13. this.phoneno = phoneno;
  14. this.designation = designation;
  15. }
  16. }
  17. // getter and setters

我已经创建了另一个用于分组的类:

  1. public class GroupHere {
  2. public static void main(String arg[]) {
  3. Function<Employee, List<Object>> classifier = (emp) ->
  4. Arrays.<Object>asList(emp.getAddress(), emp.getName());
  5. Map<Employee, Long> groupedEmployee = employee.stream()
  6. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  7. System.out.println("key" + groupedEmployee.keySet());
  8. System.out.println("Grouped by" + groupedEmployee);
  9. }
  10. }

现在我想要动态创建我的分类器,所以当用户选择了“name”和“phoneno”时,分类器应该包含这些属性的 getter 方法。

我在这里感到困惑。

此外,当用户选择属性/列时,详细信息以 JSON 格式给出。

  1. {
  2. [ attributes: name, groupable: true ],
  3. [ attributes: phoneno, groupable: true ]
  4. }

我如何将 attributes 映射到 Employeegetters 中,并添加到 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:

  1. package com.demo;
  2. public class Employee {
  3. int id;
  4. String name;
  5. String address;
  6. String phoneno;
  7. String designation;
  8. public Employee(int id, String name, String address, String phoneno, String designation) {
  9. super();
  10. this.id = id;
  11. this.name = name;
  12. this.address = address;
  13. this.phoneno = phoneno;
  14. this.designation = designation;
  15. }
  16. }
  17. // getter and setters

I have created another class for grouping

  1. public class GroupHere {
  2. public static void main(String arg[]) {
  3. Function&lt;Employee, List&lt;Object&gt;&gt; classifier = (emp) -&gt;
  4. Arrays.&lt;Object&gt;asList(emp.getAddress(),emp.getName());
  5. Map&lt;Employee, Long&gt; groupedEmployee = employee.stream()
  6. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  7. System.out.println(&quot;key&quot; + groupedEmployee .keySet());
  8. System.out.println(&quot;Grouped by&quot; + groupedEmployee );
  9. }
  10. }

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.

  1. {
  2. [ attributes: name, groupable: true ],
  3. [ attributes: phoneno, groupable: true ]
  4. }

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

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

  1. Map<String, Function<Employee, Object>> extractors = new HashMap<>();
  2. extractors.put("name", Employee::getName);
  3. extractors.put("address", Employee::getAddress);
  4. extractors.put("phoneno", Employee::getPhoneno);
  5. extractors.put("designation", Employee::getDesignation);

一旦你有了这个映射,你可以使用它动态地创建分类器:

  1. List<String> attributes = ...; // 从 JSON 中获取属性(留作练习)
  2. Function<Employee, List<Object>> classifier = emp -> attributes.stream()
  3. .map(attr -> extractors.get(attr).apply(emp))
  4. .collect(Collectors.toList());

现在,你可以使用这个分类器来分组员工:

  1. Map<List<Object>, Long> groupedEmployees = employees.stream()
  2. .collect(Collectors.groupingBy(classifier, Collectors.counting()));

这样,你就可以使用这个分类器来分组员工了。

英文:

You could do it by using a map from attribute names to attribute extractors, i.e:

  1. Map&lt;String, Function&lt;Employee, Object&gt;&gt; extractors = new HashMap&lt;&gt;();
  2. extractors.put(&quot;name&quot;, Employee::getName);
  3. extractors.put(&quot;address&quot;, Employee::getAddress);
  4. extractors.put(&quot;phoneno&quot;, Employee::getPhoneno);
  5. extractors.put(&quot;designation&quot;, Employee::getDesignation);

Once you have this map, you can use it to create your classifier dynamically:

  1. List&lt;String&gt; attributes = ...; // get attributes from JSON (left as exercise)
  2. Function&lt;Employee, List&lt;Object&gt;&gt; classifier = emp -&gt; attributes.stream()
  3. .map(attr -&gt; extractors.get(attr).apply(emp))
  4. .collect(Collectors.toList());

Now, you can use this classifier to gruop the employees:

  1. Map&lt;List&lt;Object&gt;, Long&gt; groupedEmployees = employees.stream()
  2. .collect(Collectors.groupingBy(classifier, Collectors.counting()));

huangapple
  • 本文由 发表于 2020年10月2日 00:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64159520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定