Using the stream api.
Removed unnecessary if statement.
Added a test case.
No need for the map to be sorted.
Use HashMap instead of TreeMap.
Eliminated the intermediary list.
Just use Integer::compare instead of importing Comparator.