Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy map method of collections

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) 
like image 230
deamon Avatar asked Jan 19 '11 10:01

deamon


People also ask

How do I iterate a map in Groovy?

If you wanted to do it that way, iterate over map. keySet() and the rest will work as you expected. It should work if you use s. key & s.

Are Groovy maps ordered?

2. The each Method. In Groovy, maps created with the literal notation are ordered.

What is Groovy collection?

Groovy - collect()The method collect iterates through a collection, converting each element into a new value using the closure as the transformer.

How do you check if an element is present in an array in Groovy?

Groovy - Lists contains() Returns true if this List contains the specified value.


1 Answers

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

like image 129
IttayD Avatar answered Sep 22 '22 01:09

IttayD