Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
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)
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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() }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!