Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
我发现他原来的说法是真的。仅当您覆盖迭代器中的删除时,removeAll 才会自动工作。仅仅覆盖 Collection 中的删除是不够的,因为removeAll(以及clear和retainAll)都使用迭代器来工作。由于除了迭代器中的remove之外,在使用迭代器时不应更改底层集合,因此如果不重写迭代器中的remove,则removeAll、clear和retainAll将不起作用。如果您在迭代器内的删除方法中抛出 UnsupportedOperationException,那么您在调用所讨论的三个方法之一时将看到该异常。
I have found that his original statement it true. removeAll works automatically only if you override the remove in the iterator. Just overriding the remove in the Collection is not enough because the removeAll (and clear and retainAll) all use the iterator to work. Since you should not change the underlying collection while using the iterator except for the remove in the iterator, if you do not override the remove in the iterator, the removeAll, clear and retainAll will not work. If you throw an UnsupportedOperationException in the remove method inside the iterator, that is what you will see if you call one of the three methods discussed.
你说:
正如其他人所说, .removeAll() 应该适用于您描述的场景,即使对于自定义对象,只要自定义对象遵守 Java Collections 对其对象期望的约定,包括正确实现 equals() 和 hashCode()方法。
听起来你是尝试不同的方法:编码一种方法,尝试它,快速编码另一种方法,尝试它,编码另一种方法,......值得放慢速度并尝试理解为什么每种方法失败和/或确定为什么该方法会成功。在继续下一个之前,请先尝试适合您的情况。如果您已经调查并确定了每种方法不起作用的原因,请在您的问题中进行解释。如果您还没有,那么让我们通过发布代码来提供帮助。
既然大多数人都同意第一种方法 (.removeall()) 应该有效,并且由于涉及自定义对象,为什么不快速回顾一下这个 StackOverflow 问题,看看是否有什么问题从你身上跳出来:
You said:
As others have stated, .removeAll() should work for the scenario you described, even for custom objects, as long as the custom objects obey the contracts that Java Collections expects of its objects, including a properly implement equals() and hashCode() method.
It sounds like your are shot-gunning different approaches: coding one, trying it, quickly coding another, trying it, coding yet another, ... It make be worthwhile to slow down and try to understand why each approach failed and/or determine why that approach won't work for your situation, before moving on to the next. If you've already investigated and determined why each approach won't work, please explain in your question. If you haven't, then let us help by posting code.
Since most people agree the first approach (.removeall()) should have work and since custom objects are involved, why not take a quick review of this StackOverflow question to see if anything jumps out of you:
重写
equals
和hashCode
方法足以使方法removeAll
对自定义对象起作用。您可能没有以正确的方式覆盖它们。一些代码会对我们有很大帮助。
Overriding
equals
andhashCode
methods is enough to make methodremoveAll
work on custom objects.It's likely that you didn't override them in a proper way. Some code will help us a lot.
Java 集合已经满足您的场景。调用
Collection.removeAll(Collection)
它将使用equals()
方法从传入的集合中删除所有项目来测试相等性。为了使这项工作有效,您存储的对象只需要正确实现 equals/hashCode 契约,即:给定任意两个对象
a
和b
:并且:
定义不正确equals 和 hashCode 方法会产生未定义的行为,并且是集合相关问题的常见原因。
Java Collections already cater for your scenario. Call
Collection.removeAll(Collection)
and it'll remove all the items from the passed in collection using theequals()
method to test for equality.To make this work the objects you're storing just need to properly implement the equals/hashCode contract, which is: given any two objects
a
andb
:and:
Improperly defined equals and hashCode methods create undefined behaviour and are the common cause of collections related issues.