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();
}
About | Sitemap | Contact