Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

423
Visualizações
How to merge lists of objects, based on time key

Say I have multiple (MANY) Lists, each containing multiple Record objects that contain one long constructor parameter "time" (and a string parameter for debugging purposes).

Here is an example:

val list1 = listOf(Record(0, "A1"), Record(1, "A2"), Record(2, "A3"))
val list2 = listOf(Record(0, "B1"), Record(2, "B2"))
val list3 = listOf(Record(1, "C1"), Record(2, "C2"))

I want to combine the lists into one list, so that at any given time present in the original lists, the latest value from each list is present in the list.

Here is an example:

val output = listOf(Record(0, "A1+B1"), Record(1, "A2+B1+C1"), Record(2, "A3+B2+C2"))

Assume that Records can be added to create new Records containing the data of both.

(Technically, Records are a typealias for a key-value map containing data, but I thought that was out of scope for this question.)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

you can use 'associateByTo' inline method. Assuming that:

data class Record(val time: Int, var text: String)

then:

    val list1 = listOf(Record(0, "A1"), Record(1, "A2"), Record(2, "A3"))
    val list2 = listOf(Record(0, "B1"), Record(2, "B2"))
    val list3 = listOf(Record(1, "C1"), Record(2, "C2"))

    val map = hashMapOf<Int, Record>()
    val output = listOf(list1, list2, list3).flatten().associateByTo(map,
        { it.time },
        {
            //if the record id exist in map, append time
            map[it.time]?.apply { text += "+${it.text}" } ?: it
        })

    println(output.values)

output.values are what you want.

over 4 years ago · Santiago Trujillo Relatório

0

Assuming that

data class Record(val time:Long , val param2:String)

Then you can do like this

val list1 = listOf(Record(0, "A1"), Record(1, "A2"), Record(2, "A3"))
val list2 = listOf(Record(0, "B1"), Record(2, "B2"))
val list3 = listOf(Record(1, "C1"), Record(2, "C2"))

val output = listOf(list1,list2,list3)
             .flatten()
             .groupBy { it.time }
             .map { (key,value) -> 
                    // you could define your own separator if needed
                   val newParam2 = value.joinToString(separator = "+") { it.param2 }
                   Record(key,newParam2)

              }

println(output)
//[Record(0, "A1+B1"), Record(1, "A2+B1+C1"), Record(2, "A3+B2+C2")]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda