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

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}