如何迭代 SortedSet 以修改其中的项目
假设我有一个列表。在 for 循环中修改列表的项目没有问题:
for (int i = 0; i < list.size(); i++) { list.get(i).setId(i); }
但我有一个 SortedSet 而不是列表。我怎样才能用它做同样的事情? 谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,
Set
假设它的元素是不可变的(实际上,可变元素是允许的,但它们必须遵守一个非常具体的约定,我怀疑你的类会这样做) 。这意味着通常您无法像处理列表那样就地修改集合元素。
Set
支持的两个基本操作是添加和删除元素。修改可以被认为是删除旧元素,然后添加新元素:Iterator.remove()
;Set.addAll()
位于末尾。First of all,
Set
assumes that its elements are immutable (actually, mutable elements are permitted, but they must adhere to a very specific contract, which I doubt your class does).This means that generally you can't modify a set element in-place like you're doing with the list.
The two basic operations that a
Set
supports are the addition and removal of elements. A modification can be thought of as a removal of the old element followed by the addition of the new one:Iterator.remove()
;Set.addAll()
at the end.您无法修改集合的键,因为它会导致集合重新排序/重新排序。因此,迭代将如何进一步运行将是未定义的行为。
您可以使用 iterator.remove() 删除元素。但你不能添加元素,通常更好的解决方案是将它们累积在一个新集合中,并在迭代后 addAll 。
You cannot modify set's key, because it causes the set rehasing/reordering. So, it will be undefined behaviour how the iteration will run further.
You could remove elements using iterator.remove(). But you cannot add elements, usually better solution is to accumulate them in a new collection and addAll it after the iteration.
从 Java 1.6 开始,您可以使用
NavigableSet
。Since Java 1.6, you're able to use a
NavigableSet
.您应该使用
迭代器
< /a> 或更好的是增强的 for 循环语法(这取决于实现Iterable
接口),无论Collection
您正在使用。这抽象了用于遍历集合的机制,并允许在不影响迭代例程的情况下替换新的实现。例如:
EDIT
Kan 在修改项目键方面提出了很好的观点。假设您的类的
equals()
和hashCode()
方法仅基于“id”属性(您正在更改),最安全的方法是显式删除当您迭代时,这些来自Set
并将它们添加到“输出”Set
中;例如You should use an
Iterator
or better still the enhanced for-loop syntax (which depends on the class implementing theIterable
interface), irrespective of theCollection
you're using. This abstracts away the mechanism used to traverse the collection and allows a new implementation to be substituted in without affecting the iteration routine.For example:
EDIT
Kan makes a good point regarding modifying the item's key. Assuming that your class's
equals()
andhashCode()
methods are based solely on the "id" attribute (which you're changing) the safest approach would be to explicitly remove these from theSet
as you iterate and add them to an "output"Set
; e.g.你不能那样做。但你可以尝试一下,也许你会成功,也许你会得到
ConcurrentModificationException
。记住这一点非常重要,在迭代时修改元素可能会产生意想不到的结果。您应该将这些元素收集到某个集合中。并在迭代后一一修改。You cannot do that. But you may try, maybe you'll succeed, maybe you'll get
ConcurrentModificationException
. It's very important to remember, that modifying elements while iterating may have unexpected results. You should instead collect that elements in some collection. And after the iteration modify them one by one.仅当
id
未用于 equals 或用于排序集的比较器时,这才有效:This will only work, if
id
is not used for equals, or the comperator you used for the sorted set: