Groovy map method of collections

ScalaGroovyFunctional ProgrammingMap

Scala Problem Overview


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)

Scala Solutions


Solution 1 - Scala

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

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

http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html#_iterating_on_a_list

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeamonView Question on Stackoverflow
Solution 1 - ScalaIttayDView Answer on Stackoverflow