How to apply Filtering on groupBy in java streams(如何在 java 流中对 groupBy 应用过滤)
问题描述
如何先分组,然后使用 Java 流应用过滤?
How do you group first and then apply filtering using Java streams?
示例:考虑这个 Employee
类:我想按部门分组,列出工资大于 2000 的员工.
Example: Consider this Employee
class:
I want to group by Department with a list of an employee having a salary greater than 2000.
public class Employee {
private String department;
private Integer salary;
private String name;
//getter and setter
public Employee(String department, Integer salary, String name) {
this.department = department;
this.salary = salary;
this.name = name;
}
}
我可以这样做
List<Employee> list = new ArrayList<>();
list.add(new Employee("A", 5000, "A1"));
list.add(new Employee("B", 1000, "B1"));
list.add(new Employee("C", 6000, "C1"));
list.add(new Employee("C", 7000, "C2"));
Map<String, List<Employee>> collect = list.stream()
.filter(e -> e.getSalary() > 2000)
.collect(Collectors.groupingBy(Employee::getDepartment));
输出
{A=[Employee [department=A, salary=5000, name=A1]],
C=[Employee [department=C, salary=6000, name=C1], Employee [department=C, salary=7000, name=C2]]}
由于 B 部门没有工资大于 2000 的员工.所以没有 B 部门的密钥:但实际上,我想要那个带有空列表的键 –
As there are no employees in Department B with a salary greater than 2000. So there is no key for Department B: But actually, I want to have that key with empty list –
预期输出
{A=[Employee [department=A, salary=5000, name=A1]],
B=[],
C=[Employee [department=C, salary=6000, name=C1], Employee [department=C, salary=7000, name=C2]]}
我们该怎么做?
推荐答案
nullpointer's answer 显示了直接的方法去.如果你不能更新到 Java 9,没问题,这个 filtering
收集器没有什么魔力.这是一个 Java 8 兼容版本:
nullpointer’s answer shows the straight-forward way to go. If you can’t update to Java 9, no problem, this filtering
collector is no magic. Here is a Java 8 compatible version:
public static <T, A, R> Collector<T, ?, R> filtering(
Predicate<? super T> predicate, Collector<? super T, A, R> downstream) {
BiConsumer<A, ? super T> accumulator = downstream.accumulator();
return Collector.of(downstream.supplier(),
(r, t) -> { if(predicate.test(t)) accumulator.accept(r, t); },
downstream.combiner(), downstream.finisher(),
downstream.characteristics().toArray(new Collector.Characteristics[0]));
}
您可以将它添加到您的代码库中,并以与 Java 9 对应的相同方式使用它,因此如果您使用 import static
,则无需以任何方式更改代码.
You can add it to your codebase and use it the same way as Java 9’s counterpart, so you don’t have to change the code in any way if you’re using import static
.
这篇关于如何在 java 流中对 groupBy 应用过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 java 流中对 groupBy 应用过滤
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01