如何获取 SortedSet 的下一个元素?

发布于 2024-09-03 09:48:19 字数 417 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

于我来说 2024-09-10 09:48:20

迭代器解决方案:

您可能应该有这样的内容:

class WindowLauncherClass {

   SortedSet set = null;
   Iterator setIterator = null;

   public WindowLauncherClass(SortedSet set) {
      this.set = set; // or you can copy it if that's what you need.
   }

   protected void launchWindow(Object item) {
     // impl 
   }

   public void onActivityResult() {
      if ( setIterator != null && setIterator.hasNext() ) 
      {   
         launchWindow(setIterator.next());
      }
   }

   public void start() {
       setIterator = set.iterator();
       onActivityResult();
   }
}

在评论中出现了有关集合更新的问题。迭代器会看到它吗?
正常的答案取决于应用程序的要求。在这种情况下,我没有所有信息,我会尝试猜测。

  1. 在 jdk 1.5 之前,只有一种 SortedSet 实现( TreeSet )。它有一个快速失败迭代器。

  2. 在jdk 6中出现了一个新的实现:ConcurrentSkipListSet。此排序集的迭代器不是快速失败迭代器。

如果您要向集合中添加的元素比当前显示的元素“小”,那么您无论如何都无法通过“好”(不是快速失败)迭代器看到它。如果您添加的元素比当前显示的元素“更大”,您将通过适当的迭代器看到它。

最终的解决方案是在创建适当的更改时实际重置集合和迭代器。通过使用 ConcurrentSkipListSet 最初您只会看到“较大”的更改,而通过使用 TreeSet 您将在每次更新时失败。

如果您愿意错过比当前更新“更小的”更新,那么请使用 jdk 6.0 和 ConcurrentSkipListSet。如果没有,您将必须跟踪所显示的内容,并使用新项目和未显示的项目重建正确的集合。

The iterator solution:

You should probably have something like this:

class WindowLauncherClass {

   SortedSet set = null;
   Iterator setIterator = null;

   public WindowLauncherClass(SortedSet set) {
      this.set = set; // or you can copy it if that's what you need.
   }

   protected void launchWindow(Object item) {
     // impl 
   }

   public void onActivityResult() {
      if ( setIterator != null && setIterator.hasNext() ) 
      {   
         launchWindow(setIterator.next());
      }
   }

   public void start() {
       setIterator = set.iterator();
       onActivityResult();
   }
}

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.

  1. until jdk 1.5 there was only one SortedSet implementstion ( TreeSet ). this had a fail fast iterator.

  2. 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.

生生漫 2024-09-10 09:48:20

除非您使用第三方库中的某些 SortedSet,否则您的集合也是一个 NavigableSetjava.lang.DataSet 中的每个 SortedSet)。 util 还实现了 NavigableSet)。如果您可以使事件传回刚刚完成处理的元素,NavigableSet 有一个方法 higher 这将使下一个元素高于您传入的元素:

public void onActivityResult(Event event) {
  Element element = event.processedElement;
  Element next = set.higher(element);

  if(next != null)
    launchWindow(next);
}

Unless you're using some SortedSet from a third-party library, your set is also a NavigableSet (every SortedSet in java.util also implements NavigableSet). 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:

public void onActivityResult(Event event) {
  Element element = event.processedElement;
  Element next = set.higher(element);

  if(next != null)
    launchWindow(next);
}
撩心不撩汉 2024-09-10 09:48:19

您应该传递 Iterator 而不是 Set,然后下一个消费者只需调用 next()

Instead of the Set you should pass the Iterator, then next consumer would just call next()

千笙结 2024-09-10 09:48:19

您不想在 SortedSet 上使用 Iterator 吗?

Don't you want to use an Iterator on the SortedSet?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文