使用收集器按两个字段分组
Group by two fields using Collectors
我有一个java对象记录:
public Record(ZonedDateTime day, int ptid, String name, String category, int amount) {
this.day= day;
this.id= id;
this.name = name;
this.category = category;
this.amount = amount;
}
我正在按 day
对 Record
的列表进行分组,然后创建一个新的 Record
,它结合了 amount
字段和 return是一张地图:
Map<ZonedDateTime, Record> map = tempList.stream().collect(Collectors.groupingBy(Record::getDay,
Collectors.collectingAndThen(
Collectors.reducing((r1, r2) -> new Record(r1.getDay(),Integer.toString(r1.getId),r1.getName(),
r1.getCategory(),r1.getAmount() + r2.getAmount())),
Optional::get)));
我想按 day
和 category
对列表进行分组。因此,如果 day
和 category
相同,我想像我已经在做的那样将 amount
字段合并到一个新的 Record
中。我需要添加另一个 Collectors.groupingBy
子句,但语法一直不起作用。我相信 return 类型会是 Map<ZonedDateTime, Map<String, List<Record>>>
。然后我还需要将 returned 地图转换为 List
.
我一直在尝试摆脱这个例子 Group by multiple field names in java 8
您可以使用 Collectors.toMap
:
来简化整个结构
Map<List<Object>, Record> map = tempList.stream()
.collect(Collectors.toMap(
r -> List.of(r.getDay(), r.getCategory()), // or Arrays.asList
Record::new,
Record::merge));
诀窍是按复合键分组。在这种情况下,我们将 List<Object>
与 Record.day
和 Record.category
一起使用。 (List
根据需要实现 Object.hashCode
和 Object.equals
,因此它可以安全地用作任何 Map
的密钥)。
为了减少工作,我们需要一个复制构造函数和一个 merge
方法:
public Record(Record r) {
this(r.day, r.name, r.name, r.category, r.amount);
}
public Record merge(Record r) {
this.amount += r.amount;
return this;
}
最后,对于 return 记录列表,没有必要做任何比这更花哨的事情了:
List<Record> result = new ArrayList<>(map.values());
我有一个java对象记录:
public Record(ZonedDateTime day, int ptid, String name, String category, int amount) {
this.day= day;
this.id= id;
this.name = name;
this.category = category;
this.amount = amount;
}
我正在按 day
对 Record
的列表进行分组,然后创建一个新的 Record
,它结合了 amount
字段和 return是一张地图:
Map<ZonedDateTime, Record> map = tempList.stream().collect(Collectors.groupingBy(Record::getDay,
Collectors.collectingAndThen(
Collectors.reducing((r1, r2) -> new Record(r1.getDay(),Integer.toString(r1.getId),r1.getName(),
r1.getCategory(),r1.getAmount() + r2.getAmount())),
Optional::get)));
我想按 day
和 category
对列表进行分组。因此,如果 day
和 category
相同,我想像我已经在做的那样将 amount
字段合并到一个新的 Record
中。我需要添加另一个 Collectors.groupingBy
子句,但语法一直不起作用。我相信 return 类型会是 Map<ZonedDateTime, Map<String, List<Record>>>
。然后我还需要将 returned 地图转换为 List
.
我一直在尝试摆脱这个例子 Group by multiple field names in java 8
您可以使用 Collectors.toMap
:
Map<List<Object>, Record> map = tempList.stream()
.collect(Collectors.toMap(
r -> List.of(r.getDay(), r.getCategory()), // or Arrays.asList
Record::new,
Record::merge));
诀窍是按复合键分组。在这种情况下,我们将 List<Object>
与 Record.day
和 Record.category
一起使用。 (List
根据需要实现 Object.hashCode
和 Object.equals
,因此它可以安全地用作任何 Map
的密钥)。
为了减少工作,我们需要一个复制构造函数和一个 merge
方法:
public Record(Record r) {
this(r.day, r.name, r.name, r.category, r.amount);
}
public Record merge(Record r) {
this.amount += r.amount;
return this;
}
最后,对于 return 记录列表,没有必要做任何比这更花哨的事情了:
List<Record> result = new ArrayList<>(map.values());