Java, Interview Questions, Programming, Software Development.
map.get(key1).compareTo(map.get(key2)); // If values are equal, compare keys lexicographically if (valueCompare == 0) { return key1.compareTo(key2); } return valueCompare; // Return comparison result } } And instantiate TreeMap with Custom Comparator: import java.util.HashMap; import java.util.Map;