0
Follow
5
View

List of Lists GroupBy

dsqlvxin 注册会员
2023-01-26 07:38

If I understood your question correctly, this could be a way to do what you want:

val passengersToTrips = trips
    .flatMap { ip ->
        // Associate every passenger in a trip to the trip: this results in a List>
        trip.passengers.map { passenger -> passenger to trip }
    }
    .groupBy(
        // Group using the passenger as key (use passenger.name in case the name should be the key)
        keySelector = { (passenger, _) -> passenger },
        // Extracting only the trip from the pair to transform from List into List 
        valueTransform = { (_, trip) -> trip }
    )

About the Author

Question Info

Publish Time
2023-01-26 07:38
Update Time
2023-01-26 07:38

Related Question