How do I traverse a sorted set backwards?

John Zukowski

Just keep getting the last element and the head set before it:

if (!set.isEmpty()) {
  Object last = set.last();
  boolean first = true;
  do {
    if (!first) {
      System.out.print(", ");
    }
    System.out.print(last);
    last=set.headSet(last).last();
    first=false;
  } while (last != set.first());
  System.out.println();
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact