Groovy has lots of awesome features which helps a lot in every day development, thanks @groovy. This blog demonstrate a cool feature of groovy list.

Get element from last in a groovy list

Lets have a list

List list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We can easily get first and last element from list:

assert list.first() == 1
assert list.last() == 10

We can get element from starting by their index:

assert list[0] == 1
assert list[1] == 2
assert list.get(4) == 5
assert list.get(5) == 6
assert list.getAt(8) == 9
assert list.getAt(9) == 10​

But how can we get element from last?

We can get element from last by negative index:

assert list[-1] == 10
assert list[-2] == 9
assert list.getAt(-8) == 3
assert list.getAt(-10) == 1

Hope this helps 🙂 .