简体   繁体   English

groovy removeAll闭包不删除列表中的元素

[英]groovy removeAll closure not removing elements in list

I'm not sure how removeAll in groovy works, but I expected this to return [40289454470ea94601470ea977d00018] 我不确定如何在groovy中删除allAll,但我希望这会返回[40289454470ea94601470ea977d00018]

def list = ['40289454470ea94601470ea977b20014', '40289454470ea94601470ea977d00018']
def list2 = ['40289454470ea94601470ea977b20014']

list.removeAll {
   list2
}

println list

but instead it returns [] 但它返回[]

please enlighten :( 请指教:(

removeAll with a Closure removes every element that the closure returns true for 带有Closure的removeAll删除了闭包返回true的每个元素

list2 coerces to true under groovy truth as it isn't empty so your code removes everything list2在groovy真理下强制执行,因为它不是空的,所以你的代码会删除所有内容

Try 尝试

list1 -= list2

Instead of using removeAll closure, you should simply use the removeAll method. 您应该只使用removeAll方法,而不是使用removeAll闭包。

list.removeAll {
   list2
}

The way with closure to go : 关闭的方式:

list.removeAll {
   list2.contains(it)
}

If list2 contains this element, then it's removed from list 如果list2包含此元素,则将其从list删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM