scala-Groovy集合的地图方法

Groovy中是否有map方法? 我想对以下Scala代码段执行类似的操作:

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 asked 2019-10-13T23:26:31Z
1个解决方案
76 votes

在groovy中有这样一种方法,称为collect,例如:

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]

IttayD answered 2019-10-13T23:26:48Z
translate from https://stackoverflow.com:/questions/4734434/groovy-map-method-of-collections