Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Groovy Goodness: Pop And Push Items In a List

DZone's Guide to

Groovy Goodness: Pop And Push Items In a List

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Groovy adds the pop and push methods to theList class. With the pop method we remove the last element of the list. And with the push method we add an element to the end of the list.

def list = ['Groovy', 'is', 'great!']
// Remove last item from list
// with pop().
assert list.pop() == 'great!'
assert list == ['Groovy', 'is']
// Remove last item
// which is now 'is'.
list.pop()
// Add new item to end of
// the list (equivalent for add()).
list.push('rocks!')
assert list == ['Groovy', 'rocks!']

Code written with Groovy 2.4

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}