我有如下代码片段,我想忽略/删除条件检查的else部分中的列表中的值。offerRecords.remove(tariffOffer)似乎不起作用
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
}发布于 2013-05-24 16:39:29
只需在处理之前过滤您的列表:
def filteredList = handsetData.findAll{handset.size() > 0}并处理过滤后的结果。顺便说一下,我不能理解each{} body中的handset是什么,但是我猜你已经明白了。
发布于 2013-05-24 22:39:33
这是典型的java并发修改。
也就是说,你不能在遍历列表的时候修改它。
除了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'] == listToTesthttps://stackoverflow.com/questions/16730738
复制相似问题