php小编草莓为您带来Java 8中流按3个字段分组并按总和聚合其他两个字段的问答。在Java编程中,流是一种新的处理集合的方式,通过使用流可以更加方便地对数据进行操作和处理。本文将详细介绍如何使用Java 8的流功能来实现按照3个字段进行分组,并对其他两个字段进行总和聚合的操作。让我们一起来探索这个有趣的问题吧!
我是 java 8 的新手,在实现类似问题上已提供的解决方案时遇到了困难。请帮忙。
在 java 8 中,如何对三个字段进行分组,该字段返回多个行,这些行必须对其余两个整数字段进行求和。 在下面的 dto/pojo 类中,需要根据 uuid、msgdate 和通道组合的唯一键对传入计数和传出计数字段进行求和。
public class reportdata { private string uuid; private string msgdate; private string channel; private integer incomingcount; private integer outgoingcount; }
//初始化列表作为示例。
List<ReportData> list1 = new ArrayList<>(); list1.add(new ReportData("c9c3a519","December 2023", "digital", 5, 0 )); list1.add(new ReportData("c9c3a519","December 2023", "digital", 3, 0 )); list1.add(new ReportData("c9c3a519","December 2023", "digital", 0, 3 )); list1.add(new ReportData("c9c3a519","November 2023", "digital", 4, 0 )); list1.add(new ReportData("c9c3a519","November 2023", "digital", 0, 4 )); list1.add(new ReportData("c9c3a519","December 2023", "manual", 5, 0 )); list1.add(new ReportData("c9c3a519","December 2023", "manual", 3, 0 )); list1.add(new ReportData("c9c3a519","December 2023", "manual", 0, 3 )); list1.add(new ReportData("c9c3a519","November 2023", "manual", 4, 0 )); list1.add(new ReportData("c9c3a519","November 2023", "manual", 0, 4 )); list1.add(new ReportData("3de4c44f","December 2023", "digital", 5, 0 )); list1.add(new ReportData("3de4c44f","December 2023", "digital", 0, 3 )); list1.add(new ReportData("3de4c44f","November 2023", "digital", 4, 0 )); list1.add(new ReportData("3de4c44f","November 2023", "digital", 0, 4 )); list1.add(new ReportData("3de4c44f","December 2023", "manual", 5, 0 )); list1.add(new ReportData("3de4c44f","December 2023", "manual", 0, 3 )); list1.add(new ReportData("3de4c44f","November 2023", "manual", 4, 0 )); list1.add(new ReportData("3de4c44f","November 2023", "manual", 0, 4 ));
输出对象应包含以下数据:
uuid msgdate 通道传入计数传出计数
c9c3a519 2023 年 12 月数字 8 3
c9c3a519 2023 年 11 月数字 4 4
c9c3a519 2023 年 12 月手册 8 3
c9c3a519 2023 年 11 月手册 4 4
...
...
...
将结果收集到地图中。此示例将使用 Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapFactory)。
此外,为了简洁起见,我使用 lombok 注释。
首先创建类来表示要分组的键和聚合数据:
@allargsconstructor @getter public class count { private final int in; private final int out; public count merge(count other) { return new count(this.in + other.in, this.out + other.out); } @override public string tostring() { return in + " " + out; } }
@allargsconstructor public class key { private final string uuid; private final string date; private final string channel; @override public int hashcode() { return objects.hash(uuid, date, channel); } @override public boolean equals(object obj) { if (this == obj) { return true; } if (!(obj instanceof key)) { return false; } key other = (key) obj; return uuid.equals(other.uuid) && date.equals(other.date) && channel.equals(other.channel); } @override public string tostring() { return uuid + " " + date + " " + channel; } }
然后使用另外 2 个方法扩展 reportdata
来创建密钥和初始聚合:
@allargsconstructor public class reportdata { //the fields public key createkey() { return new key(uuid, msgdate, channel); } public count createcount() { return new count(incomingcount, outgoingcount); } }
并收集数据:
public class somain { public static void main(string[] args) { list<reportdata> list = new arraylist<>(); //populate the list map<key, count> result = list.stream() .collect(collectors.tomap(reportdata::createkey, reportdata::createcount, count::merge, linkedhashmap::new)); for (map.entry<key, count> entry : result.entryset()) { system.out.println(entry.getkey() + " " + entry.getvalue()); } } }
收集器的参数如下:
reportdata::createkey
- 创建分组依据的键(地图的键)reportdata::createcount
- 从单个 reportdata
(地图的值)创建初始聚合count::merge
- 在按键冲突时合并两个 count
(请参阅合并方法)linkedhashmap::new
- 用于插入结果的 map
的工厂。我想保留插入顺序,但如果不需要,可以省略该参数以使用默认工厂。打印:
c9c3a519 December 2023 digital 8 3 c9c3a519 November 2023 digital 4 4 c9c3a519 December 2023 manual 8 3 c9c3a519 November 2023 manual 4 4 3de4c44f December 2023 digital 5 3 3de4c44f November 2023 digital 4 4 3de4c44f December 2023 manual 5 3 3de4c44f November 2023 manual 4 4
以上是Java 8流按3个字段分组并按总和聚合其他两个字段的详细内容。更多信息请关注PHP中文网其他相关文章!