https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Colton Idle

12/12/2023, 6:40 AM
Not sure if I'm missing something here but I have
data class Person(val phoneNumbers: List<String>, val metaData: String)
and then a list of people ( ie
val people: List<Person>
) and I wanna map it into a single list of phone numbers. That's easy right?
people.flatMap{ it.phoneNumbers }
cool. that makes sense. but now I actually want to flat map it into a list of phone numbers, and with each phone number I want to attach a piece of metaData so the final type is List<PairString, String. Any ideas?
r

Riccardo Lippolis

12/12/2023, 6:43 AM
something like:
people.flatMap { person -> person.phoneNumbers.map { it to person.metaData } }
?
or
person.metaData to it
, depending on which side of the Pair the phonenumber should be
c

Colton Idle

12/12/2023, 6:47 AM
Phone number should be on the left, and meta data on the right. Hm. Could it be as simple as a map inside of the flatmap!? let me try 🙏
yeah, i think that just about does it. thank you so much @Riccardo Lippolis i wouldn't have thought of that. i was just looking for a different operator in general. lol
👍 1
j

Javier

12/12/2023, 8:15 AM
Do you have repeated phones? If not, probably you should go with a Map and use any group API
c

Colton Idle

12/12/2023, 8:22 AM
id have to check actually, but thanks for the idea Javier
2 Views