collections

[Java] Reverse ordering a TreeMap

TreeMap is a Map that keep the element in the natural order of the key. The order can be changed providing to the contructor of the TreeMap a Comparable. So you can implement your custom one for more specific tasks. However if you only need a reverse order, you can easily use the beautifulCollections class with his reverseOrder().

Here is a quick example

Map reverseOrderedMap = new TreeMap(Collections.reverseOrder());

Here it is. Easy! :)

Advertisements