修改使用Collectors.groupingBy的过滤器方法

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

modifying filter method using collectors.groupingby

问题

我正在从一个 .txt 文件中读取数据,然后尝试根据工资范围对员工进行分组。然后展示该组员工的人数和平均工资。我到目前为止尝试过使用 list.stream().filter 方法。然而,我需要使用 collectors.groupingBy、collectors.summarizingDouble() 和 collectors.average() 方法对它们进行分组。但是我似乎做不到。以下是我目前为止所做的,
getSalary 方法:

public BigDecimal getSalary() {
    return Salary;
}

驱动类:

Map<Object, Long> avgSalary =
    employee_list.stream().collect(Collectors.groupingBy(emp -> emp.getSalary().doubleValue() < 30000, Collectors.counting()));

Set<Entry<Object, Long>> entrySet = avgSalary.entrySet();

for (Entry<Object, Long> entry : entrySet) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

我真的很感谢能够用 collectors.groupingBy() 替代 filter 方法的帮助。输出可以像这样:
<30000 : 1 : 平均工资 : 23000
40000-50000 : 2 : 平均工资 : 44000

英文:

I'm reading from a .txtfile and then I'm trying to group employees based on a salary range. Then showing the number of employees and the average salary of that group. What I've tried so far is, using the list.stream().filter method. However, I need to group them using the collectors.groupingBy, and a collectors.summarizingDouble() and collectors.average() method. But I can't seem to do it. Here's what I've done so far, <br>
the getSalary method:

public BigDecimal getSalary() {
		return Salary;
	}

the driver class:

  Map&lt;Object, Long&gt; avgSalary=
						 employee_list.stream().collect(Collectors.groupingBy(emp-&gt;emp.getSalary().doubleValue() &lt; 30000, Collectors.counting()));
						          
						 Set&lt;Entry&lt;Object, Long&gt;&gt; entrySet = avgSalary.entrySet();
						          
						 for (Entry&lt;Object, Long&gt; entry : entrySet) 
						 {
						     System.out.println(entry.getKey()+&quot; : &quot;+entry.getValue());
						 }
 

I'd really appreciate some help to replace the filter method with collectors.groupuingBy(). The output can be like:<br>
<30000 : 1 : avg salary : 23000 <br>
40000-50000 : 2 : avg salary : 44000

答案1

得分: 1

你应该使用 Collectors.summarizingDouble 并清楚地建立每个分组:

Map<String, DoubleSummaryStatistics> result = employee_list.stream()
    .collect(Collectors.groupingBy(emp ->
             emp.getSalary().doubleValue() < 30000 ?
                 "<30000" :
             emp.getSalary().doubleValue() < 40000 ?
                 "30000-40000" :
             emp.getSalary().doubleValue() < 50000 ?
                 "40000-50000" :
                 ">50000",
             Collectors.summarizingDouble(emp -> emp.getSalary().doubleValue())));

然后,通过键名简单地访问每个分组,例如 &quot;40000-50000&quot; 并获取其统计信息:

DoubleSummaryStatistics stats40k50k = result.get("40000-50000");

每个分组的所有统计信息都被收集到 DoubleSummaryStatistics 对象中,你可以从中获取例如平均值:

double avg40k50k = stats40k50k == null ? 0.0 : stats40k50k.getAverage();
英文:

You should use Collectors.summarizingDouble and establish each group clearly:

Map&lt;String, DoubleSummaryStatistics&gt; result = employee_list.stream()
    .collect(Collectors.groupingBy(emp -&gt; 
             emp.getSalary().doubleValue() &lt; 30000 ?
                 &quot;&lt;30000&quot; :
             emp.getSalary().doubleValue() &lt; 40000 ?
                 &quot;30000-40000&quot; :
             emp.getSalary().doubleValue() &lt; 50000 ?
                 &quot;40000-50000&quot; :
                 &quot;&gt;50000&quot;,
             Collectors.summarizingDouble(emp -&gt; emp.getSalary().doubleValue())));

Then, simply access each group by key, i.e. &quot;40000-50000&quot; and get its statistics:

DoubleSummaryStatistics stats40k50k = result.get(&quot;40000-50000&quot;);

All statistics for each group are collected to DoubleSummaryStatistics objects, from where you can get i.e. the average:

double avg40k50k = stats40k50k == null ? 0.0 : stats40k50k.getAverage();

huangapple
  • 本文由 发表于 2020年9月21日 22:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994511.html
匿名

发表评论

匿名网友

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

确定