Table of Content
How to use custom comparator for TreeMap
This article covers some of the aspects of sorting Map in Java. Java Map comes in different flavors, like HashMap, TreeMap, LinkedHashMap etc. TreeMap for instance will sort the data
based on the keys. We can also pass a custom Comparator to sort based on the keys as per the custom sort algorithm.
TreeMap sorts based on natural ordering of the keys if no custom Comparator is provided.
To use a custom Comparator we need to pass the Comparator in the TreeMap Constructor.
Simple TreeMap
TreeMap without any Comparator
Following code creates a TreeMap and adds some data to it. After that we print the TreeMap. As No comparator is specified, the data is sorted by natural ordering of the keys.
TreeMap map = new TreeMap();
map.put(1, "A");
map.put(3, "C");
map.put(2, "B");
map.put(4, "D");
map.put(5, "E");
System.out.println("\nPrint Map ");
map.forEach((a, b) -> {
printKeyVal(a,b);
});;
Print Map
Key: 1 Value: A Key: 2 Value: B Key: 3 Value: C Key: 4 Value: D Key: 5 Value: E
TreeMap with Comparator
TreeMap with reverse order Comparator
In this case we are passing a built in Comparator to sort the keys reversely. From the result we can see that the data is sorted in reverse order of the keys.
System.out.println("\nPrint Map with Reverse Comparator");
TreeMap map2 = new TreeMap(Collections.reverseOrder());
map2.putAll(map);
map2.forEach((a, b) -> {
printKeyVal(a, b);
});
Print Map with Reverse Comparator
Key: 5 Value: E Key: 4 Value: D Key: 3 Value: C Key: 2 Value: B Key: 1 Value: A
Custom Comparator
Following examples shows implementation of two custom Comparator, one with inner class
second one with java 8 Lambda. Both the Comparator works exactly same, that is it sorts the data in reverse order.
With Inner Classes
Custom Comparator with Inner Class to sort the keys in reverse order.
Comparator orderByKeyDesc = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
};
Lambda Comparator.
Java 8 Lambda to implement Comparator
Comparator orderByKeyDesc2 = (Integer o1, Integer o2) -> o2.compareTo(o1);
Full Example
TreeMapCustomComparator
Full example with Custom Comparator that is passed to the TreeMap.
public class TreeMapCustomComparator {
public static void main(String[] args) {
TreeMap map = new TreeMap();
map.put(1, "A");
map.put(3, "C");
map.put(2, "B");
map.put(4, "D");
map.put(5, "E");
System.out.println("\nPrint Map ");
map.forEach((a, b) -> {
printKeyVal(a, b);
});;
System.out.println("\nPrint Map with Reverse Comparator");
TreeMap map2 = new TreeMap(Collections.reverseOrder());
map2.putAll(map);
map2.forEach((a, b) -> {
printKeyVal(a, b);
});
//Custom Comparator with Inner Class
Comparator orderByKeyDesc = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
};
//Custom Comparator with Lambda
Comparator orderByKeyDesc2 = (Integer o1, Integer o2) -> o2.compareTo(o1);
System.out.println("\nPrint Map with Custom Reverse Comparator Java Lambda");
TreeMap map3 = new TreeMap(orderByKeyDesc2);
map3.putAll(map);
map3.forEach((a, b) -> {
printKeyVal(a, b);
});
}
/* Utility method to print key value pairs nicely */
private static void printKeyVal(Object a, Object b) {
System.out.print("Key: " + a + " Value: " + b + " ");
}
}
Print Map
Key: 1 Value: A Key: 2 Value: B Key: 3 Value: C Key: 4 Value: D Key: 5 Value: E
Print Map with Reverse Comparator
Key: 5 Value: E Key: 4 Value: D Key: 3 Value: C Key: 2 Value: B Key: 1 Value: A
Print Map with Custom Reverse Comparator Java Lambda
Key: 5 Value: E Key: 4 Value: D Key: 3 Value: C Key: 2 Value: B Key: 1 Value: A
No comments :
Post a Comment
Please leave your message queries or suggetions.