amin mohammadi
amin mohammadi

Reputation: 1053

kotlin; group by multiple fields

How can I do a groupBy in my code by three or more fields? My code is like below:

val nozzleSaleReport = nozzleStateList.groupBy {
    {it.shift.id},{it.createUser.id},{it.nozzle.id} // Here I need to add these three fields for grouping operation
}.map { entry ->
    val max: Float = (entry.value.maxBy { it.nozzleState.finalLitreMechanical }?.nozzleState!!.finalLitreMechanical ?: 0).toString().toFloat()
    val min: Float = (entry.value.minBy { it.nozzleState.finalLitreMechanical }?.nozzleState!!.finalLitreMechanical ?: 0).toString().toFloat()

    NozzleSaleReport(entry.value[0].createUser.name, entry.value[0].shift.name,  (max - min).toInt(),entry.value[0].shift.id, entry.value[0].nozzle.id, entry.value[0].nozzle.name)
}.let {
    println(it)
}

Upvotes: 21

Views: 24525

Answers (2)

Jaffar Abbas
Jaffar Abbas

Reputation: 41

You can use a Pair or Triple(in your usecase) provided by Kotlin. Here is a sample code that might be of some help:

items = items.groupBy { Pair(it.productId, it.batchId) }
        .values
        .map {
            it.reduce { accumulator, item ->
                item.copy(quantity = accumulator.quantity + item.quantity)
            }
        }

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691635

Let's say the class of the elements of your collection is NozzleState.

You want to group nozzle states by shift ID, create user ID and nozzle ID.

If I understand correctly, you thus want a different group for each distinct combination of shift ID, create user ID and nozzle ID.

So you need to create a class representing such a combination (let's name if Key), and group the elements by their Key:

data class Key(val shiftId: String, val createUserId: String, val nozzleId: String)
fun NozzleState.toKey() = Key(shift.id, createUser.id, nozzle.id)

nozzleStateList.groupBy { it.toKey() }

Upvotes: 38

Related Questions