我可以使用 groupingBy 以声明方式执行此操作吗?
Can I do this declaratively using groupingBy?
我有一个 Employee
class:
public class Employee {
public enum Sex {
MALE, FEMALE
}
private final String name;
private final Sex gender;
private final LocalDate dateOfBirth;
private final double salary;
private final List<String> roles;
// Constructor
public Employee(String name, Sex gender, LocalDate dateOfBirth, double salary, List<String> roles) {
this.name = name;
this.gender = gender;
this.dateOfBirth = dateOfBirth;
this.salary = salary;
this.roles = roles;
}
// Getters & toString omitted
public static List<Employee> listOfStaff() {
List<Employee> staff = new ArrayList<>();
staff.add(new Employee("Jack", Employee.Sex.MALE,
IsoChronology.INSTANCE.date(1954, 12, 2), 49999.00,
new ArrayList<>(Arrays.asList("Manager", "Director"))));
staff.add(new Employee("Jill", Employee.Sex.FEMALE,
IsoChronology.INSTANCE.date(1995, 10, 25), 24999.00,
new ArrayList<>(Arrays.asList("Secretary", "Manager", "Personnel"))));
staff.add(new Employee("Dorothy", Employee.Sex.FEMALE,
IsoChronology.INSTANCE.date(1972, 4, 7), 21999.00,
new ArrayList<>(Arrays.asList("Secretary", "Receptionist"))));
staff.add(new Employee("Bert", Employee.Sex.MALE,
IsoChronology.INSTANCE.date(1968, 11, 5), 21999.00,
new ArrayList<>(Arrays.asList("Clerk", "Receptionist"))));
staff.add(new Employee("Mary", Employee.Sex.FEMALE,
IsoChronology.INSTANCE.date(2001, 12, 3), 16999.00,
new ArrayList<>(Arrays.asList("Trainee", "Receptionist"))));
staff.add(new Employee("Matthew", Employee.Sex.MALE,
IsoChronology.INSTANCE.date(1962, 4, 7), 12999.00,
new ArrayList<>(Arrays.asList("Personnel", "Receptionist"))));
return staff;
}
}
我可以迭代地提取角色和角色中的人员:
public class TestGrouping {
/* Grouping streams */
public static void main(String[] args) {
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee e : Employee.listOfStaff()) {
List<String> roles = e.getRoles();
List<String> names = new ArrayList<>();
names.add(e.getName());
for (String r : roles) {
if(roleAndNames.get(r) == null) {
roleAndNames.put(r, names);
} else {
roleAndNames.get(r).add(e.getName());
}
}
}
for (Map.Entry<String, List<String>> entry : roleAndNames.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue().toString());
}
}
}
这给了我以下输出:
我曾尝试使用流以声明方式做同样的事情,但似乎无法获得相同的结果。
我以为我可以使用 groupingBy() 和 collectingAndThen() 来做到这一点,但我并不高兴!
这可能吗?
这里是一步一步的解决方案:
- 您可以使用
flatMap
获取所有可能的“角色名称”对 (Map.Entry<String, String>
)。
- 使用
Collectors.groupingBy
收集到 Map
结构中,使用额外的 Collectors.mapping
下游收集器提取平面映射“角色名称”对的 name
。它需要另一个将这些名称打包成 List
.
Map<String, List<String>> map = Employee.listOfStaff().stream()
.flatMap(employee -> employee.getRoles()
.stream()
.map(role -> new AbstractMap.SimpleEntry<>(role, employee.getName())))
.collect(Collectors.groupingBy(
Entry::getKey,
Collectors.mapping(Entry::getValue, Collectors.toList())));
map.forEach((k,v) -> System.out.println(k + ":" + v));
我看到您从 Java 9 开始使用 java-8. If you later switch to a newer Java, you can use Map.entry(..)
而不是 new AbstractMap.SimpleEntry(...)
。
编辑: 如果您不想使用 java-stream,修复您当前的迭代代码,您需要修复 List
insertion/update 到地图(两个片段工作相同):
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee employee: Employee.listOfStaff()) {
for (String role: employee.getRoles()) {
List<String> names = new ArrayList<>();
if (roleAndNames.containsKey(role)) {
names = roleAndNames.get(role);
}
names.add(employee.getName());
roleAndNames.put(role, names);
}
}
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee employee: Employee.listOfStaff()) {
for (String role: employee.getRoles()) {
List<String> names = roleAndNames.getOrDefault(role, new ArrayList<>());
names.add(employee.getName());
roleAndNames.put(role, names);
}
}
... 或更好地使用 Map#computeIfAbsent
,如果没有任何值,它会创建一个新条目并且总是 returns 值(这是一个列表,可以是空列表,也可以是带有一些名称的列表).因此,您总是使用它在每次迭代中添加名称。
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee employee: Employee.listOfStaff()) {
for (String role: employee.getRoles()) {
roleAndNames.computeIfAbsent(role, roleKey -> new ArrayList<>())
.add(employee.getName());
}
}
仅此而已:) 我喜欢这种方法及其 return 类型。非常好用!
诀窍是首先 'explode' 将您的输入输入叉积:您希望每个 person/role 组合在流中有 1 个条目。完成后,您可以使用 groupingBy
操作将其折叠回以 Role 为键的映射。想想数据库和 SQL 如果您熟悉它。
Employee.listOfStaff().stream()
.flatMap(employee -> employee.getRoles().stream());
这为您提供横切 (flatMap 'maps' 流中的 1 个条目到 0-x 条目,方法是为它提供一个新流。flatMap 生成的所有流都连接成一个新流,它是当你在 flatMap 操作之后写 .foo()
时,你在操作什么。
从这里您可以进行相当基本的操作 Collectors.groupingBy
以获得所需的结果。
仅供参考,这个:
public enum Sex {
MALE, FEMALE
}
不是个好主意。现在是 2021 年,而不是 1970 年。除非别无他法,否则不要提出性要求。例如,如果你收集这个是因为你希望该信息可用,所以你可以将 'Mr' 或 'Mrs' 放在字母的顶部,然后去掉枚举和字段,取而代之的是一个'honorific'。如果你愿意,可以做一个枚举。我只想用一个字符串。各种政府实体断然不能使用您的软件,或者至少会非常抵制它。他们有指令要遵守,说在不需要时停止询问,而且很少需要。
我有一个 Employee
class:
public class Employee {
public enum Sex {
MALE, FEMALE
}
private final String name;
private final Sex gender;
private final LocalDate dateOfBirth;
private final double salary;
private final List<String> roles;
// Constructor
public Employee(String name, Sex gender, LocalDate dateOfBirth, double salary, List<String> roles) {
this.name = name;
this.gender = gender;
this.dateOfBirth = dateOfBirth;
this.salary = salary;
this.roles = roles;
}
// Getters & toString omitted
public static List<Employee> listOfStaff() {
List<Employee> staff = new ArrayList<>();
staff.add(new Employee("Jack", Employee.Sex.MALE,
IsoChronology.INSTANCE.date(1954, 12, 2), 49999.00,
new ArrayList<>(Arrays.asList("Manager", "Director"))));
staff.add(new Employee("Jill", Employee.Sex.FEMALE,
IsoChronology.INSTANCE.date(1995, 10, 25), 24999.00,
new ArrayList<>(Arrays.asList("Secretary", "Manager", "Personnel"))));
staff.add(new Employee("Dorothy", Employee.Sex.FEMALE,
IsoChronology.INSTANCE.date(1972, 4, 7), 21999.00,
new ArrayList<>(Arrays.asList("Secretary", "Receptionist"))));
staff.add(new Employee("Bert", Employee.Sex.MALE,
IsoChronology.INSTANCE.date(1968, 11, 5), 21999.00,
new ArrayList<>(Arrays.asList("Clerk", "Receptionist"))));
staff.add(new Employee("Mary", Employee.Sex.FEMALE,
IsoChronology.INSTANCE.date(2001, 12, 3), 16999.00,
new ArrayList<>(Arrays.asList("Trainee", "Receptionist"))));
staff.add(new Employee("Matthew", Employee.Sex.MALE,
IsoChronology.INSTANCE.date(1962, 4, 7), 12999.00,
new ArrayList<>(Arrays.asList("Personnel", "Receptionist"))));
return staff;
}
}
我可以迭代地提取角色和角色中的人员:
public class TestGrouping {
/* Grouping streams */
public static void main(String[] args) {
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee e : Employee.listOfStaff()) {
List<String> roles = e.getRoles();
List<String> names = new ArrayList<>();
names.add(e.getName());
for (String r : roles) {
if(roleAndNames.get(r) == null) {
roleAndNames.put(r, names);
} else {
roleAndNames.get(r).add(e.getName());
}
}
}
for (Map.Entry<String, List<String>> entry : roleAndNames.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue().toString());
}
}
}
这给了我以下输出:
我曾尝试使用流以声明方式做同样的事情,但似乎无法获得相同的结果。
我以为我可以使用 groupingBy() 和 collectingAndThen() 来做到这一点,但我并不高兴!
这可能吗?
这里是一步一步的解决方案:
- 您可以使用
flatMap
获取所有可能的“角色名称”对 (Map.Entry<String, String>
)。 - 使用
Collectors.groupingBy
收集到Map
结构中,使用额外的Collectors.mapping
下游收集器提取平面映射“角色名称”对的name
。它需要另一个将这些名称打包成List
.
Map<String, List<String>> map = Employee.listOfStaff().stream()
.flatMap(employee -> employee.getRoles()
.stream()
.map(role -> new AbstractMap.SimpleEntry<>(role, employee.getName())))
.collect(Collectors.groupingBy(
Entry::getKey,
Collectors.mapping(Entry::getValue, Collectors.toList())));
map.forEach((k,v) -> System.out.println(k + ":" + v));
我看到您从 Java 9 开始使用 java-8. If you later switch to a newer Java, you can use Map.entry(..)
而不是 new AbstractMap.SimpleEntry(...)
。
编辑: 如果您不想使用 java-stream,修复您当前的迭代代码,您需要修复 List
insertion/update 到地图(两个片段工作相同):
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee employee: Employee.listOfStaff()) {
for (String role: employee.getRoles()) {
List<String> names = new ArrayList<>();
if (roleAndNames.containsKey(role)) {
names = roleAndNames.get(role);
}
names.add(employee.getName());
roleAndNames.put(role, names);
}
}
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee employee: Employee.listOfStaff()) {
for (String role: employee.getRoles()) {
List<String> names = roleAndNames.getOrDefault(role, new ArrayList<>());
names.add(employee.getName());
roleAndNames.put(role, names);
}
}
... 或更好地使用 Map#computeIfAbsent
,如果没有任何值,它会创建一个新条目并且总是 returns 值(这是一个列表,可以是空列表,也可以是带有一些名称的列表).因此,您总是使用它在每次迭代中添加名称。
Map<String, List<String>> roleAndNames = new HashMap<>();
for (Employee employee: Employee.listOfStaff()) {
for (String role: employee.getRoles()) {
roleAndNames.computeIfAbsent(role, roleKey -> new ArrayList<>())
.add(employee.getName());
}
}
仅此而已:) 我喜欢这种方法及其 return 类型。非常好用!
诀窍是首先 'explode' 将您的输入输入叉积:您希望每个 person/role 组合在流中有 1 个条目。完成后,您可以使用 groupingBy
操作将其折叠回以 Role 为键的映射。想想数据库和 SQL 如果您熟悉它。
Employee.listOfStaff().stream()
.flatMap(employee -> employee.getRoles().stream());
这为您提供横切 (flatMap 'maps' 流中的 1 个条目到 0-x 条目,方法是为它提供一个新流。flatMap 生成的所有流都连接成一个新流,它是当你在 flatMap 操作之后写 .foo()
时,你在操作什么。
从这里您可以进行相当基本的操作 Collectors.groupingBy
以获得所需的结果。
仅供参考,这个:
public enum Sex {
MALE, FEMALE
}
不是个好主意。现在是 2021 年,而不是 1970 年。除非别无他法,否则不要提出性要求。例如,如果你收集这个是因为你希望该信息可用,所以你可以将 'Mr' 或 'Mrs' 放在字母的顶部,然后去掉枚举和字段,取而代之的是一个'honorific'。如果你愿意,可以做一个枚举。我只想用一个字符串。各种政府实体断然不能使用您的软件,或者至少会非常抵制它。他们有指令要遵守,说在不需要时停止询问,而且很少需要。