如何获取 SortedSet 的下一个元素?
我有一个 SortedSet 保存我的有序数据。
我使用 .first()
方法返回第一条记录,并将其传递到另一个窗口。
当另一个窗口完成时,我会调用一个事件,并且我想将下一个元素从 SortedSet 传递到窗口,那么如何移动到下一个元素?
launchWindow(this.set.first());
然后我有这个:
onActivityResult(...) {
if (this.set.hasNext()) launchWindow(this.set.next());//hasNext/next doesn't exists in the current context for SortedSet
}
我有什么选择?
I have a SortedSet holding my ordered data.
I use the .first()
method to return the first record, and pass it to another window.
When the other window finishes I get an event called, and I want to pass the next from the SortedSet to the window, so how to move to the next element?
launchWindow(this.set.first());
Then I have this:
onActivityResult(...) {
if (this.set.hasNext()) launchWindow(this.set.next());//hasNext/next doesn't exists in the current context for SortedSet
}
What options I have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
迭代器解决方案:
您可能应该有这样的内容:
在评论中出现了有关集合更新的问题。迭代器会看到它吗?
正常的答案取决于应用程序的要求。在这种情况下,我没有所有信息,我会尝试猜测。
在 jdk 1.5 之前,只有一种 SortedSet 实现( TreeSet )。它有一个快速失败迭代器。
在jdk 6中出现了一个新的实现:ConcurrentSkipListSet。此排序集的迭代器不是快速失败迭代器。
如果您要向集合中添加的元素比当前显示的元素“小”,那么您无论如何都无法通过“好”(不是快速失败)迭代器看到它。如果您添加的元素比当前显示的元素“更大”,您将通过适当的迭代器看到它。
最终的解决方案是在创建适当的更改时实际重置集合和迭代器。通过使用 ConcurrentSkipListSet 最初您只会看到“较大”的更改,而通过使用 TreeSet 您将在每次更新时失败。
如果您愿意错过比当前更新“更小的”更新,那么请使用 jdk 6.0 和 ConcurrentSkipListSet。如果没有,您将必须跟踪所显示的内容,并使用新项目和未显示的项目重建正确的集合。
The iterator solution:
You should probably have something like this:
In the comments appeared the question about updates to the set. Will the iterator see it ?.
The normal answer is depends on the application requirements. In this case i don't have all the information and i'll try to guess.
until jdk 1.5 there was only one SortedSet implementstion ( TreeSet ). this had a fail fast iterator.
in jdk 6 appeared a new implementation: ConcurrentSkipListSet. The iterator for this sorted set is not a fail fast one.
If you are adding an element into the set that is "smaller" than the currently displayed element then you will not be able to see it anyway by a "good" (not fail fast) iterator. If you are adding an element "bigger" that the currently displayed element you will see it by a proper iterator.
The final solution is to actually reset the set and the iterator when a proper change is created. By using a ConcurrentSkipListSet initially you will see only the "bigger" changes and by using a TreeSet you will fail at every update.
If you afford to miss updates "smaller" than the current one then go for the jdk 6.0 and ConcurrentSkipListSet. If not than you'll have to keep track of what you displayed and rebuild a proper set with new items and undisplayed items.
除非您使用第三方库中的某些
SortedSet
,否则您的集合也是一个NavigableSet
(java.lang.DataSet 中的每个
还实现了SortedSet
)。 utilNavigableSet
)。如果您可以使事件传回刚刚完成处理的元素,NavigableSet
有一个方法 higher 这将使下一个元素高于您传入的元素:Unless you're using some
SortedSet
from a third-party library, your set is also aNavigableSet
(everySortedSet
injava.util
also implementsNavigableSet
). If you can make the event pass back the element it just finished working on,NavigableSet
has a method higher which will get the next element higher than the one you pass in:您应该传递 Iterator 而不是 Set,然后下一个消费者只需调用 next()
Instead of the Set you should pass the Iterator, then next consumer would just call next()
您不想在
SortedSet
上使用Iterator
吗?Don't you want to use an
Iterator
on theSortedSet
?