Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda