How can I get the next value of iterator while looping through a collection with Groovy 1.7.4
values.each {it ->
println(it)
println(it.next()) //wrong
}
Another way to get access to the previous element is with List.collate. By setting the step parameter to 1, you can get a "sliding window" view of your collection:
def windowSize = 2
def values = [ 1, 2, 3, 4 ]
[null, *values].collate(windowSize, 1, false).each { prev, curr ->
println "$prev, $curr"
}
The list has to be padded with null at the beginning to provide the first element's prev.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With