Search

Dark theme | Light theme

June 13, 2018

Groovy Goodness: Remove Last Item From List Using RemoveLast Method (And Pop/Push Methods Reimplemented)

Versions of Groovy before 2.5.0 implemented pop and push methods for the List class for items at the end of a List object. The pop method removed the last item of a List and 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 removeLast.

In the following example Groovy code we use the removeLast and add methods to remove and add items to the end of the list. And with the pop and 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'.
list.removeLast()
 
// Add new item to end of the list.
list.add 'rocks!'
 
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.
list.push 'Spock'

assert list.join(' ') == 'Spock rocks!'

Written with Groovy 2.5.0.
{"cc_re__cc_re__cc_by_4_0": ["tical-align:middle;border:0\"></a>\n<a href='https://feeds.feedburner.com/mrhaki' rel='alternate' type='application/rss+xml'>Subscribe in a reader</a>\n</p>\n<p id='license' xmlns:cc='http://creativecommons.org/ns#' xmlns:dct='http://purl.org/dc/terms/'><a href='https://blog.mrhaki.com/' property='dct:title' rel='cc:attributionURL'>Messages from mrhaki</a> by <a href='https://www.mrhaki.com/' property='cc:attributionName' rel='cc:attributionURL dct:creator'>Hubert Klein Ikkink</a> is licensed under <a href='http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1' rel='license noopener noreferrer' style='display:inline-block;' target='_blank'>Attribution 4.0 International<img src='https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1' style='height:22px!important;margin-left:3px;vertical-align:text-bottom;'/><img src='https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1' style='height:22px!important;margin-left:3px;vertical-align:text-bottom;'/></a>\n</p>\n</footer>\n<script language='javascript' src='//www.mrhaki.com/js/syntax/shCore.js'></script>\n<script language='javascript' src='//www.mrhaki.co"], "copyright__copyright__copyright": ["moveLast()\n \n// Add new item to end of the list.\nlist.add 'rocks!'\n \nassert list.join(' ') == 'Groovy rocks!'\n\n\n/* IMPORTANT */\n/* pop() and push() implementations has changed */\n/* in Groovy 2.5.0. They now work on the first */\n/* item in a List instead of the last. */\n\n// Using pop() we remove the first item\n// of a List.\nassert list.pop() == 'Groovy'\n\n// And with push we add item to \n// beginning of a List.\nlist.push 'Spock'\n\nassert list.join(' ') == 'Spock rocks!'\n\nWritten with Groovy 2.5.0."]}