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

277
Visualizações
Kotlin convert csv to map <String, List>

I have following CSV

US;Americas
CA;Americas
FR;Europe
CH;Asia
...

I want to have Map of -> String, List, is it possible to achieve this via associate, or something else ?

So far I have this:

csv.split("\n").associate { 
  val (code, region) = it.split(";") 
  region to code 
}

but this is just mapping the region to only one code, also tried

region to listOf(code)

expected result:

Americas -> US, CA, BR ...
Europe -> FR, IT, CH ...
Asia -> CH, JP, KR ...
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

csv
  .split("\n")
  .groupBy({ it.substringAfter(";") }) { it.substringBefore(";") }})
over 4 years ago · Santiago Trujillo Relatório

0

associate would replace the value if the key already iterated. You can instead use groupBy to specify key and value for the map

csv.split("\n").map {
    val (code, region) = it.split(";")
    region to code
}.groupBy({ it.first }) { it.second }
over 4 years ago · Santiago Trujillo Relatório

0

Alternatively to sidgate's excellent answer you could also do

csv.split("\n").groupBy({
    it.split(";")[1]
}){
    it.split(";")[0]
}

It avoids having to create a map first, but calls split more often. I'm not sure what would be better in terms of speed/efficiency.

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