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

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

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 映射到 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:

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&lt;Employee, List&lt;Object&gt;&gt; classifier = (emp) -&gt;
	    Arrays.&lt;Object&gt;asList(emp.getAddress(),emp.getName());

Map&lt;Employee, Long&gt; groupedEmployee = employee.stream()
				  .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

System.out.println(&quot;key&quot; + groupedEmployee .keySet());
System.out.println(&quot;Grouped by&quot; + 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&lt;String, Function&lt;Employee, Object&gt;&gt; extractors = new HashMap&lt;&gt;();
extractors.put(&quot;name&quot;, Employee::getName);
extractors.put(&quot;address&quot;, Employee::getAddress);
extractors.put(&quot;phoneno&quot;, Employee::getPhoneno);
extractors.put(&quot;designation&quot;, Employee::getDesignation);

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

List&lt;String&gt; attributes = ...; // get attributes from JSON (left as exercise)

Function&lt;Employee, List&lt;Object&gt;&gt; classifier = emp -&gt; attributes.stream()
    .map(attr -&gt; extractors.get(attr).apply(emp))
    .collect(Collectors.toList());

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

Map&lt;List&lt;Object&gt;, Long&gt; groupedEmployees = employees.stream()
    .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:

确定