2013-05-24 5 views
0

다음과 같이 코드 스 니펫이 있습니다. 조건 검사의 else 부분에서 값을 무시하거나 제거하고 싶습니다. offerRecords.remove (tariffOffer) 나던Groovy 목록에서 특정 값 제거

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 
     } 
+0

'handset'은 무엇입니까 :

한 예는 반복이 완료되면 원래의 목록에서를 누른 다음, 잘못된 항목의 병렬 목록을 계속 제거하는 것? 우리가 실행할 수있는 몇 가지 코드를 생각해 내시겠습니까? 나는 당신이'findAll'을 원한다고 의심한다. –

+0

def 핸드셋 = [], 핸드셋은 목록이다. 내 코드 조각이 내 전체 프로그램의 아주 작은 부분이기 때문에, 독립적으로 실행할 수있는 샘플을주는 것이 어렵다. – Techie

답변

1

그냥 처리하기 전에 목록을 필터링 제대로 작동 :

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

및 프로세스 필터링 된 결과를. 그건 그렇고, 내가 handseteach{} 본문에 이해가 안 돼요,하지만 당신은 생각이있어.

+0

findAll doesnt 조건에 따라 목록을 필터링하려면 hmmm – Techie

+0

findAll은 필터링 된 결과를 반환하고 컬렉션 자체에는 영향을주지 않습니다. –

0

이는 일반적인 자바 동시 수정입니다.

즉 반복되는 동안 목록을 수정할 수 없습니다.

Cat의 이전 제안에서 반복하기 전에 제안한 것 외에도 사용 사례의 다른 요소에 따라 많은 솔루션이 있습니다 (This SO question 참조).

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

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

listToTest.removeAll invalidItems 

assert ['a', 'b', 'c'] == listToTest