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

229
Views
Valor de grupo de Kotlin como conjunto en lugar de lista

Voy a agrupar los valores de Set<Pair<String,Int>> a Map<String, Set<Int>> de manera tan simple. Encuentre todos los enteros coincidentes para la cadena y cree un conjunto. Tengo una solución que funciona:

 setStringInt.stream() .collect( groupingBy( Projection::stringObj, mapping(Projection::intObj, toSet<Int>()) ) )

Tengo una especie de limpiador:

 .groupBy { it.stringobj } .mapValues { it.value.map { it.intobj }.toSet() }

Pero se ve bastante sucio. ¿Tienes alguna idea de cómo simplificar esto? ¿Puedo hacer esto sin usar stream?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Creo que esto es más legible:

 setStringInt.groupBy({ it.first }, { it.second }).mapValues { it.value.toSet() }

utilizando el

 inline fun <T, K, V> Iterable<T>.groupBy( keySelector: (T) -> K, valueTransform: (T) -> V ): Map<K, List<V>>

sobrecarga.

over 4 years ago · Santiago Trujillo Report

0

Simplemente puede usar las funciones de Kotlin groupBy y luego mapValues directamente en el Set .

Primero usa groupBy en it.first para obtener Map<String, List<Pair<String, Int>> . Luego, debe asignar los valores del mapa resultante, por lo que obtiene un List<Int> de List<Pair<String, Int>> . Me gusta esto:

 setStringInt.groupBy { it.first } .mapValues { entry -> entry.value.map { it.second }.toSet() }
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!