Versions of Groovy before 2.5.0 implemented
push methods for the
List class for items at the end of a
List object. The
pop method removed the last item of a
push added a item to the
List. Groovy 2.5.0 reimplemented the methods so they now work on the first item of a
List instance. To remove an item from the end of the list we can use the newly added method
In the following example Groovy code we use the
add methods to remove and add items to the end of the list. And with the
push methods we remove and add items to the beginnen of the list:
def list = ['Groovy', 'is', 'great!']
// Remove last item from list
// with removeLast().
assert list.removeLast() == 'great!'
assert list == ['Groovy', 'is']
// Remove last item which is now 'is'.
// Add new item to end of the list.
assert list.join(' ') == 'Groovy rocks!'
/* IMPORTANT */
/* pop() and push() implementations has changed */
/* in Groovy 2.5.0. They now work on the first */
/* item in a List instead of the last. */
// Using pop() we remove the first item
// of a List.
assert list.pop() == 'Groovy'
// And with push we add item to
// beginning of a List.
assert list.join(' ') == 'Spock rocks!'
Written with Groovy 2.5.0.