开发者

How to get the next element of a SortedSet?

开发者 https://www.devze.com 2023-01-01 12:35 出处:网络
I have a SortedSet holding my ordered data. I use the .first() method to return the first record, and pass it to another window.

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 th开发者_如何学JAVAe 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?


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


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


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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消