5
    Map<Integer, String> map = new TreeMap<Integer, String>();

    // Add Items to the TreeMap
    map.put(new Integer(8), "Eight");
    map.put(new Integer(9), "Nine");
    map.put(new Integer(1), "One");
    map.put(new Integer(4), "Four");
    map.put(new Integer(10), "Ten");
    map.put(new Integer(5), "Five");
    map.put(new Integer(6), "Six");
    map.put(new Integer(2), "Two");
    map.put(new Integer(3), "Three");
    map.put(new Integer(7), "Seven");

    keys = map.keySet();
    for (Iterator i = keys.iterator(); i.hasNext();) {
      Integer key = (Integer) i.next();
      String value = (String) map.get(key);
      System.out.println(key + " = " + value);
    }

Output:

  • 1 = One
  • 2 = Two
  • 3 = Three
  • 4 = Four
  • 5 = Five
  • 7 = Seven
  • 8 = Eight
  • 9 = Nine
  • 10 = Ten

I would like to reverse this integer sort of the TreeMap, So the highest integer will be at the front and the lowest and the end, How can I accomplish this? Thanks in advance.

user2803086
  • 153
  • 1
  • 3
  • 10

2 Answers2

9

TreeMap's constructor can take Comparator you can pass custom implementation

Change your Map declaration to pass reverse order comparator

Map<Integer, String> map = new TreeMap<Integer, String>(Collections.reverseOrder());

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • That seems to work 'Collections.reverseOrder()', I don't have knowledge about Comparators, but this seems to be the easiest fix. – user2803086 Jul 24 '14 at 20:37
  • Comparator is an Interface that helps sorting algorithms to compare two Objects, for example you know 1 < 2, but for custom objects for example Person how do you determine if p1 > p2, Comparator helps there, see the javadoc linked – jmj Jul 24 '14 at 20:39
2

How about

NavigableMap<Integer, String> map = new TreeMap<Integer, String>();

for(Map<Integer, String> entry : map.descendingMap().entrySet())
     System.out.println(entry); // prints key = value
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • This seems to be the easiest way, since my knowledge about Comparators is close to zero. My only question is why to use NavigableMap and not TreeMap at the start? – user2803086 Jul 24 '14 at 20:33
  • 2
    @user2803086 It is considered good practice to use the higher level interface abstractions if access to the specific underlying implementing type is not directly needed in the code. The reason for this is that it makes it easy to change the implementing type should you desire to do so in the future (and you are not bound to any contracts requiring a lower level type). This is by no means a requirement and in practice it may be rare that you would actually change the implementing type of collections classes but good practice is good practice. – Trevor Freeman Jul 24 '14 at 21:10
  • You can replace TreeMap with ConcurrentSkipListMap or another such map in one place only. `NavigableMap` makes it clear this is the minimum requirement. – Peter Lawrey Jul 24 '14 at 21:14
  • @increment1 I know that List is underlaying by an ArrayList, but I never really know why. Is there any name for this practice, because I certainly want to know how this done. Thanks in advance – user2803086 Jul 25 '14 at 18:46
  • 1
    If you have `List list = new ArrayList<>();` your `list` is a *reference* to anything which implements a `List` This means you can use any other implementation without changing the code which uses it. i.e. `List list = new CopyOnWriteArrayList<>();` and nothing else needs to be changed. This is called inheritance and polymorphism. – Peter Lawrey Jul 25 '14 at 18:51