105

Is there a map method in Groovy? I want to do something like I do with the following Scala snippet:

scala> val l = List(1, 2, 3)
l: List[Int] = List(1, 2, 3)

scala> l.map(_ + 1)
res0: List[Int] = List(2, 3, 4)
deamon
  • 89,107
  • 111
  • 320
  • 448

1 Answers1

153

There is such a method in groovy, it is called collect, for example:

assert [1, 2, 3].collect { it * 2 } == [2, 4, 6]

https://groovy-lang.org/groovy-dev-kit.html#_iterating_on_a_list

Lifeweaver
  • 986
  • 8
  • 29
IttayD
  • 28,271
  • 28
  • 124
  • 178