flatMap() collections in Groovy

Viewed 576

In Kotlin and Scala we have flatMap functions which work like below:

val listOfMaps = listOf(
    mapOf("a" to 1, "b" to 2, "c" to 10),
    mapOf("a" to 3, "b" to 4, "d" to 99),
)

listOfMaps.flatMap { listOf(it["a"], it["b"]) }
[1, 2, 3, 4]

What I'm looking for is the exact same function, but in Groovy:

[[a:1,b:2,c:10],[a:3,b:4,c:99]].flatMap { [it.a, it.b] }
1 Answers

The function I need is present in Groovy, but it's name is different - collectMany.

[[a:1,b:2,c:10],[a:3,b:4,c:99]].collectMany { [it.a, it.b] }
[1, 2, 3, 4]
Related