tags:

views:

141

answers:

4

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?

+3  A: 

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

Eugene Kuleshov
If meanwhile my set was extended, will the iterator know of it?
Pentium10
@Pentium10: The answer is "It depends." If it's a `TreeSet`, yes and not in a good way; it would fail and throw a `ConcurrentModificationException`. If it's a `ConcurrentSkipListSet`, no; it will reflect the `SortedSet` as it was when you passed the iterator. Basically, `TreeSet` is not thread-safe, while `ConcurrentSkipListSet` is.
R. Bemrose
Unfortunately, `java.util.concurrent.ConcurrentSkipListSet` is new in Java 1.6.
R. Bemrose
It wasn't mentioned in the original question, but in that case you'll have to use Queue collection instead of the Set, and in order to guard against adding duplicated elements you could also keep Set
Eugene Kuleshov
+3  A: 

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

Amir Afghani
If meanwhile my set was extended, will the iterator know of it?
Pentium10
+2  A: 

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);
}
jasonmp85
And as an aside, be sure to look at the other methods that `NavigableSet` (there's also a `NavigableMap`, btw). They're pretty useful (especially `subSet`, which for values `floor` and `ceiling` returns all values `v` such that `floor` ≤ `v` < `ceiling`), but you can't use what you don't know about!
jasonmp85
I have a `Treeset` and using java 1.5 and the `higher` seams it's missing.
Pentium10
There are only two built-in `SortedSet` s, and both are also `NavigableSet` s. The other is `java.util.concurrent.ConcurrentSkipListSet`, and as its name implies, it is built with concurrency in mind.
R. Bemrose
@Pentium10: `NavigableSet` and all its methods are new in Java 1.6.
R. Bemrose
Sounds like a compelling reason to upgrade!
jasonmp85
Unfortunately I can't, I develop for Android, so I am at their hand.
Pentium10
Can you not use the Java 6 object and just target the 1.5 JVM at compile time?EDIT: Nevermind, I'm showing my Android ignorance. I suppose using NavigableSet is impossible.
jasonmp85
A: 

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.

Toader Mihai Claudiu
It does not address the issue with elements added to the set as per requester comments on the other answers.
Eugene Kuleshov
I didn't follow the complete comments history so i wasn't aware of the issue (it wasn't in the original question). Updating the post with my thought about this.
Toader Mihai Claudiu