kotlin - Kotlin - create an object based on sum
问题描述
I have a question. I have the following class
data class Item(val month: Int, val year: Int, val value: Int)
And I have a list
val items = listOf(
Item(month = 1, year = 2019, value = 5000),
Item(month = 1, year = 2019, value = 200),
Item(month = 1, year = 2019, value = 300),
Item(month = 1, year = 2019, value = 1000),
Item(month = 2, year = 2019, value = 5000),
Item(month = 2, year = 2019, value = 6000),
Item(month = 3, year = 2019, value = 500),
Item(month = 3, year = 2019, value = 1500),
Item(month = 5, year = 2019, value = 900),
Item(month = 1, year = 2020, value = 700)
)
I want to sum values of Item objects having the same month and year, and create another Item object with field month, year and sum as value
val result = listOf(
Item(month = 1, year = 2019, value = 6500),
Item(month = 2, year = 2019, value = 11000),
Item(month = 3, year = 2019, value = 2000),
Item(month = 5, year = 2019, value = 900),
Item(month = 1, year = 2020, value = 700)
)
How can I achieve so? Thanks
解决方案
首先,您按月 + 年分组(假设这应该是唯一的)。然后将其映射到一个新项目,其中的值是每个组的值。
items.groupBy { "${it.month}/${it.year}" }
会给你这样的东西:
{
'01/2019': [
Item(month = 1, year = 2019, value = 5000),
Item(month = 1, year = 2019, value = 200),
Item(month = 1, year = 2019, value = 300),
Item(month = 1, year = 2019, value = 1000)
],
'02/2019': ...
}
您现在将此分组映射映射到新项目,您可以在其中value
使用所有项目的总和重新计算。生成的代码如下所示:
items.groupBy { "${it.month}/${it.year}" }
.map {
Item(it.value.first().month,
it.value.first().year,
it.value.sumBy(Item::value))
}
我的代码中的结果与您的示例输出完全相同。
更新:
为了避免创建字符串和访问第一个数组元素,还有另一种方法。创建您分组的数据对象:
data class Date(var month: Int, var year: Int)
现在您按动态创建的 Date 对象进行分组 - 并解压缩该值:
items.groupBy({ Date(it.month, it.year) }, { it.value })
这将创建一个这样的地图:
{
{ month: 1, year: 2019 }: [ 5000, 200, 300, 1000 ],
{ month: 2, year: 2019 }: [ ... ]
}
之后,您可以以与以前类似的方式进行映射,但成员访问权限更加清晰:
items.groupBy({ Date(it.month, it.year) }, { it.value })
.map {
Item(it.key.month,
it.key.year,
it.value.sum())
}
如果您使用它作为您的项目的基类,您还可以扩展数据类的重用。
推荐阅读
- spring - 无法从 SpringBoot 以字符串或 ModelAndView 的形式返回 HTML 文件
- mysql - 使用用户模型验证用户,我在 phpmyadmin 中创建了用户表并与 django 连接。然后将其迁移到应用程序
- java - 将 SpringBoot 应用程序推送到 heroku 时预接收挂钩被拒绝
- symfony - Mercure/Symfony:使用 JWT 密钥的授权错误
- python - StartsWith 函数 Python
- ios - 数据源加载完成后,SwiftUI NavigationView 会自行弹出
- java - 从 Java 中的 JSON Schema 获取可用属性(可能是递归的)
- node.js - 如何在Node js中将自动url传递给http协议
- python - 如何将记录器信息发送到 seq
- c++ - Clion 如何添加调试标志?