groovyjenkins-groovygroovyshell

Why Groovy Pop() function is removing the first element from list?


I'm new to Groovy. I was trying Pop function, which should remove last element from the list acc to documentation. But it pops the first element in my case instead of last. Can any one tell me if that's a bug or am I missing something here?

This is the list i'm using:

def primes = [25,3,5,7,11,13];

Here is the output screenshot:

ss


Solution

  • https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html#pop()

    Removes the initial item from the List.

    def list = ["a", false, 2]
    assert list.pop() == 'a'
    assert list == [false, 2]
    

    This is similar to pop on a Stack where the first item in the list represents the top of the stack. Note: The behavior of this method changed in Groovy 2.5 to align with Java. If you need the old behavior use 'removeLast'.