Sorting TreeMap Using Comparator

TreeMap are sorted naturally using their Keys. If you have your objects as keys then Sorting TreeMap using Comparator is a good way to decide the map order.
Lets see an example on how to use Comparator in a TreeMap

1. User Defined Object –

2. Comparator – Sorting by Salary

3. Code to use Comparator


After executing the above code, we get the following into the console. You can see that the TreeMap was sorted based on the employee salary
Sorting TreeMap Using Comparator

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn