英文:
Kotlin to use groupby multiple fields and adding values
问题
我尝试使用Kotlin的内置方法来计算按"item"和"env"分组的多个项目的总成本。
我有以下类似的数据结构;
Data(id=1, cost=1, env='prod', item='Storage')
Data(id=2, cost=2, env='qa', item='Storage')
Data(id=3, cost=3, env='prod', item='Storage')
Data(id=4, cost=5, env='qa', item='Bandwidth')
Data(id=5, cost=2, env='qa', item='Bandwidth')
Data(id=6, cost=5, env='prod', item='Bandwidth')
Data(id=7, cost=6, env='prod', item='Bandwidth')
Data(id=8, cost=1, env='prod', item='Bandwidth')
Data(id=9, cost=5, env='qa', item='vm')
Data(id=10, cost=2, env='uat', item='vm')
Data(id=11, cost=3, env='qa', item='vm')
Data(id=12, cost=4, env='prod', item='vm')
Data(id=13, cost=5, env='uat', item='vm')
我想要通过"item"和"env"来添加成本。
所以预期的最终结果将是
data class TotalCost(val item: String, val env: String, val cost: Double){}
{
("Storage", "prod", 4.0)
("Storage", "qa", 2.0)
("Bandwidth", "qa", 7.0)
("Bandwith", "prod", 12.0)
("vm","qa", 8.0)
("vm", "uat", 7.0)
("vm", "prod", 4.0)
}
我尝试使用groupBy但无法获得我想要的结果。
英文:
I am trying to calculate the total costs by grouping multiple items using Kotlin's built in method
I have a data structure like following;
Data(id=1, cost=1, env='prod', item='Storage')
Data(id=2, cost=2, env='qa', item='Storage')
Data(id=3, cost=3, env='prod', item='Storage')
Data(id=4, cost=5, env='qa', item='Bandwidth')
Data(id=5, cost=2, env='qa', item='Bandwidth')
Data(id=6, cost=5, env='prod', item='Bandwidth')
Data(id=7, cost=6, env='prod', item='Bandwidth')
Data(id=8, cost=1, env='prod', item='Bandwidth')
Data(id=9, cost=5, env='qa', item='vm')
Data(id=10, cost=2, env='uat', item='vm')
Data(id=11, cost=3, env='qa', item='vm')
Data(id=12, cost=4, env='prod', item='vm')
Data(id=13, cost=5, env='uat', item='vm')
I would like to add the cost using "item" and "env"
So expected end result will be
data class TotalCost(val item: String, val env: String, val cost: Double){}
{
("Storage", "prod", 4)
("Storage", "qa", 2)
("Bandwidth", "qa", 7)
("Bandwith", "prod", 12)
("vm","qa", 8)
("vm", "uat", 7
("vm", "prod", 4)
}
I tried using groupBy but couldn't get the result i want.
答案1
得分: 0
以下是翻译好的代码部分:
data class Data(
val id: Int,
val cost: Int,
val env: String,
val item: String
)
data class TotalCost(
val item: String,
val env: String,
val cost: Double
)
val input = listOf(
Data(id = 1, cost = 1, env = "prod", item = "Storage"),
Data(id = 2, cost = 2, env = "qa", item = "Storage"),
Data(id = 3, cost = 3, env = "prod", item = "Storage"),
Data(id = 4, cost = 5, env = "qa", item = "Bandwidth"),
Data(id = 5, cost = 2, env = "qa", item = "Bandwidth"),
Data(id = 6, cost = 5, env = "prod", item = "Bandwidth"),
Data(id = 7, cost = 6, env = "prod", item = "Bandwidth"),
Data(id = 8, cost = 1, env = "prod", item = "Bandwidth"),
Data(id = 9, cost = 5, env = "qa", item = "vm"),
Data(id = 10, cost = 2, env = "uat", item = "vm"),
Data(id = 11, cost = 3, env = "qa", item = "vm"),
Data(id = 12, cost = 4, env = "prod", item = "vm"),
Data(id = 13, cost = 5, env = "uat", item = "vm")
)
val result = input
.groupBy {
it.item to it.env
}
.map { (key, value) ->
listOf(
key.first,
key.second,
value.sumOf { it.cost }
)
}
result.forEach(::println)
输出:
[Storage, prod, 4]
[Storage, qa, 2]
[Bandwidth, qa, 7]
[Bandwidth, prod, 12]
[vm, qa, 8]
[vm, uat, 7]
[vm, prod, 4]
英文:
data class Data(
val id: Int,
val cost: Int,
val env: String,
val item: String
)
data class TotalCost(
val item: String,
val env: String,
val cost: Double
)
val input = listOf(
Data(id = 1, cost = 1, env = "prod", item = "Storage"),
Data(id = 2, cost = 2, env = "qa", item = "Storage"),
Data(id = 3, cost = 3, env = "prod", item = "Storage"),
Data(id = 4, cost = 5, env = "qa", item = "Bandwidth"),
Data(id = 5, cost = 2, env = "qa", item = "Bandwidth"),
Data(id = 6, cost = 5, env = "prod", item = "Bandwidth"),
Data(id = 7, cost = 6, env = "prod", item = "Bandwidth"),
Data(id = 8, cost = 1, env = "prod", item = "Bandwidth"),
Data(id = 9, cost = 5, env = "qa", item = "vm"),
Data(id = 10, cost = 2, env = "uat", item = "vm"),
Data(id = 11, cost = 3, env = "qa", item = "vm"),
Data(id = 12, cost = 4, env = "prod", item = "vm"),
Data(id = 13, cost = 5, env = "uat", item = "vm")
)
val result = input
.groupBy {
it.item to it.env
}
.map { (key, value) ->
listOf(
key.first,
key.second,
value.sumOf { it.cost }
)
}
result.forEach(::println)
Output:
[Storage, prod, 4]
[Storage, qa, 2]
[Bandwidth, qa, 7]
[Bandwidth, prod, 12]
[vm, qa, 8]
[vm, uat, 7]
[vm, prod, 4]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论