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.