nebula186
nebula186

Reputation: 149

Kotlin flatten collections

I have a list of objects of type Range, which in turn has a property which is a list of type List<Map<*, *>>. Now, how do I collect all the elements of this property into a flat list?

data class Range(
        @JsonProperty("events") val pEvents: List<Map<*, *>>,
        @JsonProperty("gt_events") val gEvents: List<Map<String, *>>
)

How do I traverse ranges to get a list of events and gEvents? Here is something I tried

fun processEvents(val ranges: List<Range>):
 val events: List<Map<String, *>> = ranges.flatMap { g -> g.gEvents.map { it } }

Upvotes: 1

Views: 680

Answers (1)

Joffrey
Joffrey

Reputation: 37660

This should be sufficient to get a List<Map<String, *>>:

val allGEvents = ranges.flatMap { it.gEvents }

Upvotes: 1

Related Questions