.collect with an index

Since Groovy 2.4.0 there is a withIndex() method which gets added to java.lang.Iterable.

So, in a functional fashion (no side effect, immutable), it looks like

def myList = [
  [position: 0, name: 'Bob'],
  [position: 0, name: 'John'],
  [position: 0, name: 'Alex'],
]

def result = myList.withIndex().collect { element, index ->
  [position: index, name: element["name"]] 
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)