英文:
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<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());
}
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())));
然后,通过键名简单地访问每个分组,例如 "40000-50000"
并获取其统计信息:
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<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())));
Then, simply access each group by key, i.e. "40000-50000"
and get its statistics:
DoubleSummaryStatistics stats40k50k = result.get("40000-50000");
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论