Kotlin : Groupby the objects by 2 values, perform sum and then map it to other object

huangapple go评论50阅读模式
英文:

Kotlin : Groupby the objects by 2 values, perform sum and then map it to other object

问题

不需要翻译的代码部分:

i am not very expert in kotlin collection functions so need some help:
Assume i have a data class:

data class transaction(val SenderName: String,val ReceiverName: String,val Amount: Int)
the list of transactions

val transactions = listOf(
transaction("Rahul","Ravi",10000),
transaction("Rahul","Ravi",-500),
transaction("Ravi","Rahul",1000))
First i want to group the entries based on the sender & receiver followed by adding the amounts. which i am able to do using the below code

transactions.groupBy { it.SenderName to it.ReceiverName}.map{entity ->
 val amountSum = entity.value.sumOf { it.Amount }}
after this what i want is to calculate the final amount of transactions between 2 persons from one person's reference. Like for example i calculate for Rahul's point of view then:
Sum of amount(where Rahul is Sender) - Sum of amount (where rahul is receiver)
which will be: 9500 - 1000 = 8500
and the final output should be:

data class NewObject(val reference_person:String, val Other_person :String, val FinalAmount:Int)

val finalList = [("Rahul","Ravi",8500),("Ravi","Rahul",-8500)]

I have gone throught many stackoverflow posts but don't know how to achieve this. Thanks

以下是翻译好的部分:

我对 Kotlin 集合函数不是很擅长,所以需要一些帮助:

假设我有一个数据类:

data class Transaction(val senderName: String, val receiverName: String, val amount: Int)

交易列表如下:

val transactions = listOf(
    Transaction("Rahul", "Ravi", 10000),
    Transaction("Rahul", "Ravi", -500),
    Transaction("Ravi", "Rahul", 1000)
)

首先,我想根据发送者和接收者对条目进行分组,然后添加金额。我可以使用以下代码来实现:

transactions.groupBy { it.senderName to it.receiverName }.map { entity ->
    val amountSum = entity.value.sumOf { it.amount }
}

在此之后,我想要计算两个人之间的交易的最终金额,从一个人的角度出发。例如,如果我从 Rahul 的角度计算:

Rahul 作为发送者的金额总和 - Rahul 作为接收者的金额总和

这将是:9500 - 1000 = 8500

最终的输出应该如下所示:

data class NewObject(val referencePerson: String, val otherPerson: String, val finalAmount: Int)

val finalList = listOf(
    NewObject("Rahul", "Ravi", 8500),
    NewObject("Ravi", "Rahul", -8500)
)

我查阅了很多 Stack Overflow 帖子,但不知道如何实现这一目标。谢谢。

英文:

i am not very expert in kotlin collection functions so need some help:
Assume i have a data class:

data class transaction(val SenderName: String,val ReceiverName: String,val Amount: Int)

the list of transactions

val transactions = listOf(
transaction("Rahul","Ravi",10000),
transaction("Rahul","Ravi",-500),
transaction("Ravi","Rahul",1000))

First i want to group the entries based on the sender & receiver followed by adding the amounts. which i am able to do using the below code

transactions.groupBy { it.SenderName to it.ReceiverName}.map{entity ->
 val amountSum = entity.value.sumOf { it.Amount }}

after this what i want is to calculate the final amount of transactions between 2 persons from one person's reference. Like for example i calculate for Rahul's point of view then:
Sum of amount(where Rahul is Sender) - Sum of amount (where rahul is receiver)
which will be: 9500 - 1000 = 8500
and the final output should be:

data class NewObject(val reference_person:String, val Other_person :String, val FinalAmount:Int)

val finalList = [("Rahul","Ravi",8500),("Ravi","Rahul",-8500)]

I have gone throught many stackoverflow posts but don't know how to achieve this. Thanks

答案1

得分: 1

你可以尝试这样做:

val finalList = (transactions + transactions.map {
    transaction(
        it.ReceiverName,
        it.SenderName,
        -1 * it.Amount
    )
}).groupBy { it.SenderName to it.ReceiverName }.map { entity ->
    NewObject(entity.key.first, entity.key.second, entity.value.sumOf { it.Amount })
}

println(finalList)
//[NewObject(reference_person=Rahul, Other_person=Ravi, FinalAmount=8500), NewObject(reference_person=Ravi, Other_person=Rahul, FinalAmount=-8500)]

我首先将原始列表与具有发送者和接收者颠倒的相同列表相加。然后按照你的方式将它们分组,最后映射到 NewObject

英文:

You can try this

val finalList = (transactions + transactions.map {
    transaction(
        it.ReceiverName,
        it.SenderName,
        -1 * it.Amount
    )
}).groupBy { it.SenderName to it.ReceiverName }.map { entity ->
    NewObject(entity.key.first, entity.key.second, entity.value.sumOf { it.Amount })
}

println(finalList)
//[NewObject(reference_person=Rahul, Other_person=Ravi, FinalAmount=8500), NewObject(reference_person=Ravi, Other_person=Rahul, FinalAmount=-8500)]

What I first do is add to the original list the same list but with sender and receiver reversed. Then I group them like you did, and finally mapped them to NewObjects

huangapple
  • 本文由 发表于 2023年3月3日 21:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定