1

I need to iterate through a TreeMap<Integer, T> map from the end to beginning. How to do so?


I have tried the same thing as the answer in posted topic below:

new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.intValue() - o1.intValue();
    }
}

but this is not a solution, that's just reversing the order.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    a duplicate of which question? This seems to be quite different than the "How to Print a treemap in reverse order". That questions (and certainly the answers) seems to focus on how to build a treemap using a custom (eg. reverse) ordering. This is how to take a TreeMap, in whatever order it is in, and iterate it in reverse. – Brett Okken Jun 02 '14 at 11:11

1 Answers1

5

Call descendingMap and iterate over the entry set from that map.

Brett Okken
  • 6,210
  • 1
  • 19
  • 25