Over a million developers have joined DZone.

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

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

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'.
// Add new item to end of
// the list (equivalent for add()).
assert list == ['Groovy', 'rocks!']

Code written with Groovy 2.4

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}