0

I have my code snippet as below, and I would like to ignore/remove the value from the list in the else part of the condition check. offerRecords.remove(tariffOffer) doesnt seem to work

offerRecords.each { tariffOffer ->

    handsetData.each { hs ->
        if (tariffOffer.HANDSET_BAND.stringValue() == hs.HANDSET_BAND?.stringValue()) {
            //println 'condition is satisfied and set the handset id ****** '
            handset.add(hs.HANDSET_PKEY_ID?.stringValue())
        }

        if (handset.size() > 0) {
            // need to call a method
            recHandset = applyHandsetRulesCHL(tariffOffer, handset)
        }
        else {
            // ignore/remove the tariffOffer
            offerRecords.remove(tariffOffer) // i know it doesn't serve the purpose
        }
codelark
  • 12,254
  • 1
  • 45
  • 49
Techie
  • 1,611
  • 3
  • 15
  • 24
  • What's `handset`? Could you come up with some code that we can run that shows the problem? I suspect you want a `findAll` – tim_yates May 24 '13 at 08:39
  • def handset = [], handset is a list, as my code snippet is a tiny part of my whole program, it is difficult for me to give a sample which could run independently – Techie May 24 '13 at 08:46

2 Answers2

1

Just filter your list before process:

def filteredList = handsetData.findAll{handset.size() > 0}

and process filtered result. By the way, I can't understand what is handset in each{} body, but I guess you got the idea.

Mr. Cat
  • 3,522
  • 2
  • 17
  • 26
0

This is typical java concurrent modification.

i.e. You can't modify a list while iterating over it.

In addition to Mr. Cat's earlier suggestion of filtering the data prior to iteration, there are many solutions depending on other factors of your use case, see This SO question.

One example would be to keep a parallel list of invalid entries, then remove them from the original list once iteration is complete:

def listToTest = ['a', 1, 'b', 'c']

def invalidItems = []
listToTest.each {
    if (it == 1)
        invalidItems << it
}

listToTest.removeAll invalidItems

assert ['a', 'b', 'c'] == listToTest
Community
  • 1
  • 1
codelark
  • 12,254
  • 1
  • 45
  • 49