Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 503 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
kotlin; group by multiple fields

#1
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)
}
Reply

#2
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() }
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through