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

128
Views
Cómo crear un mapa de cadena para listar

Tengo datos JSON en el formato dado:

 dataset = [ { 'name': 'Sayantan', 'section': 'A', 'detail': 'complete' }, { 'name': 'Charu', 'section': 'B', 'detail': 'complete' }, { 'name': 'Sanhati', 'section': 'C', 'detail': 'inprogress' }, ]

Quiero convertirlo en un mapa de <String, List<>> y obtenerlo en el siguiente formato:

 {'complete': [{'detail': 'complete', 'name': 'Sayantan', 'section': 'A'}, {'detail': 'complete', 'name': 'Charu', 'section': 'B'}], 'inprogress': [{'detail': 'inprogress', 'name': 'Sanhati', 'section': 'C'}]}

No tengo mucha experiencia en Kotlin, he trabajado mayormente con este tipo de casos en Python y he logrado escribir lo siguiente:

 val mapOfInterest = mutableMapOf<String, List<DataResource>>() for (data in datasets) { val mapKey = data.detail if (!mapOfInterest.containsKey(mapKey)) { if (mapKey != null) { mapOfInterest[mapKey] = listOf(data) } else { mapOfInterest[mapKey] // I am stuck here } } }

No puedo agregar datos al valor clave del mapa usando add() , cualquier idea o pista sería útil.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Hay una función stdlib para lo que necesita llamada groupBy :

 val mapOfInterest = dataset.groupBy { it.detail }
over 4 years ago · Santiago Trujillo Report

0

En Kotlin, las colecciones no se pueden cambiar de forma predeterminada: https://kotlinlang.org/docs/collections-overview.html . Entonces List no tiene un método add() .

Podría usar mutableListOf() en su lugar, pero la forma "correcta" de hacerlo en Kotlin es, como ya señaló Joffrey, usar la función estándar groupBy() https://kotlinlang.org/api/latest/jvm/stdlib /kotlin.colecciones/grupo-por.html

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!