0

I'm running groovy in Jenkins. I have 2 lists that are created from a sh script. I need to format each item on list1 and then check if that value is in list2. If it is, I want to remove it from list2.

At first I was running into a lot of issues using .remove() so I've tried different implementations of .split() as List and tokenize() to avoid the No signature of method: java.util.ArrayList.remove() is applicable for argument types: (java.lang.String) values:

The problem is when I do my list2.contains(newName) it gives me false. I get true if I dont do the as List, but then I'm unable to use .remove() for reason above. I also tried to use the removeIf function, but get groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.removeIf() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2@3ab421bf]

Thanks for any help, this has been driving me crazy!

def list1 = []
def list2 = []

list1 = sh(script: """myscript""", returnStdout: true).split() as List
list2 = sh(script: """myscript""", returnStdout: true).tokenize()

list1.each { i -> 
    newName = "test-${i}"
    if (list2.contains(newName)) {
        list2.remove(newName) 
}
echo "${list2} // This doesn't change in Jenkins even when it should because the if statement isn't working.
          
            
 //Instead of if statement I've also tried:
 //chartList.removeIf { k -> k == chartName}

I've checked my classes as well:

println(list1.getClass())
> class java.util.ArrayList
println (list2.getClass())
> class java.util.ArrayList

Groovy Playground works with the removeIf and my if statement, which is how I know it must be something Jenkins.

def list1 = []
def list2 = []

list1 = ['orange', 'lime']
list2 = ['apple', 'orange', 'banana', 'pear']

list1.each { i ->

list2.removeIf { it -> it == i }

    //This also works:
       // if (chartList.contains(i)) {

       //     chartList.remove(i)
      
       // }
}
println list2
3
  • Not sure if this is related to stackoverflow.com/questions/9682206/… Commented Jun 2, 2021 at 5:05
  • additional node: in groovy list2-list1 should work. Commented Jun 2, 2021 at 5:07
  • Use "test-${i}" as String Commented Jun 2, 2021 at 7:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.