TreeMap high-low key Integer sort
Asked Answered
L

2

6
    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.

Langan answered 24/7, 2014 at 20:26 Comment(0)
H
10

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

Hypochlorite answered 24/7, 2014 at 20:27 Comment(2)
That seems to work 'Collections.reverseOrder()', I don't have knowledge about Comparators, but this seems to be the easiest fix.Langan
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 linkedHypochlorite
S
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
Shipe answered 24/7, 2014 at 20:30 Comment(5)
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?Langan
@Langan 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.Lewan
You can replace TreeMap with ConcurrentSkipListMap or another such map in one place only. NavigableMap makes it clear this is the minimum requirement.Shipe
@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 advanceLangan
If you have List<String> 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<String> list = new CopyOnWriteArrayList<>(); and nothing else needs to be changed. This is called inheritance and polymorphism.Shipe

© 2022 - 2024 — McMap. All rights reserved.