How to Sort a TreeMap By Value in Java? Last Updated : 17 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java Language, a TreeMap always stores key-value pairs which are in sorted order on the basis of the key. TreeMap implements the NavigableMap interface and extends AbstractMap class. TreeMap contains unique keys. Sorting TreeMap by value in Java The elements in TreeMap are sorted on the basis of keys.So, we need to develop our own logic to sort it on the basis of value. We can do it using comparator class Example 1: Java // Java program to Sort a TreeMap By Value import java.util.*; class GFG { public static <K, V extends Comparable<V> > Map<K, V> valueSort(final Map<K, V> map) { // Static Method with return type Map and // extending comparator class which compares values // associated with two keys Comparator<K> valueComparator = new Comparator<K>() { // return comparison results of values of // two keys public int compare(K k1, K k2) { int comp = map.get(k1).compareTo( map.get(k2)); if (comp == 0) return 1; else return comp; } }; // SortedMap created using the comparator Map<K, V> sorted = new TreeMap<K, V>(valueComparator); sorted.putAll(map); return sorted; } public static void main(String[] args) { TreeMap<String, Integer> map = new TreeMap<String, Integer>(); // Put elements to the map map.put("Anshu", 2); map.put("Rajiv", 4); map.put("Chhotu", 3); map.put("Golu", 5); map.put("Sita", 1); // Calling the method valueSort Map sortedMap = valueSort(map); // Get a set of the entries on the sorted map Set set = sortedMap.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while (i.hasNext()) { Map.Entry mp = (Map.Entry)i.next(); System.out.print(mp.getKey() + ": "); System.out.println(mp.getValue()); } } } OutputSita: 1 Anshu: 2 Chhotu: 3 Rajiv: 4 Golu: 5 Example 2: Java // Java program to Sort a TreeMap By Value import java.util.*; class GFG { // Method for sorting the TreeMap based on values public static <K, V extends Comparable<V> > Map<K, V> valueSort(final Map<K, V> map) { // Static Method with return type Map and // extending comparator class which compares values // associated with two keys Comparator<K> valueComparator = new Comparator<K>() { public int compare(K k1, K k2) { int comp = map.get(k1).compareTo(map.get(k2)); if (comp == 0) return 1; else return comp; } }; // SortedMap created using the comparator Map<K, V> sorted = new TreeMap<K, V>(valueComparator); sorted.putAll(map); return sorted; } public static void main(String[] args) { TreeMap<Integer, String> map = new TreeMap<Integer, String>(); // Put elements to the map map.put(1, "Anshu"); map.put(5, "Rajiv"); map.put(3, "Chhotu"); map.put(2, "Golu"); map.put(4, "Sita"); // Calling the method valueSort Map sortedMap = valueSort(map); // Get a set of the entries on the sorted map Set set = sortedMap.entrySet(); // Get an iterator Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry mp = (Map.Entry)i.next(); System.out.print(mp.getKey() + ": "); System.out.println(mp.getValue()); } } } Output1: Anshu 3: Chhotu 2: Golu 5: Rajiv 4: Sita Comment More infoAdvertise with us Next Article How to Compare Two TreeMap Objects in Java? A anshukumarpathak1999 Follow Improve Article Tags : Java Java Programs java-TreeMap Practice Tags : Java Similar Reads How to Get TreeMap Key or Value using Index in Java? The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using whic 5 min read How to Compare Two TreeMap Objects in Java? TreeMap class in java provides a way of storing key-value pairs in sorted order. The below example shows how to compare two TreeMap objects using the equals() method. It compares two TreeMap objects and returns true if both of the maps have the same mappings else returns false. Syntax: boolean equal 1 min read How to Merge Two TreeMaps in Java? In Java, TreeMap is a pre-defined class that implements the NavigableMap interface and extends the AbstractMap class. In this article, we will learn to Merge Two Tree Maps in Java. Program to Merge Two TreeMaps in JavaWe are using the putAll method is inherited from the AbstractMap class in this man 2 min read How to Create a TreeMap in Java and Add Key-Value Pairs in it? In Java, a TreeMap maintains elements in a sorted order as it is an implementation of the SortedMap Interface. It stores key-value pairs in a sorted order. In this article, we will explore the creation of TreeMap in Java and learn the step-by-step process of adding key-value pairs. Program to Create 2 min read How to Convert TreeMap to an ArrayList in Java? TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot 4 min read How to Sort a TreeSet with User Defined Objects in Java? Comparator interface sorts the objects of user-defined classes. An object of the Comparator class is capable of comparing two objects of two different classes. Following function compare obj1 with obj2 TreeSet implements the SortedSet interface. So, duplicate values are not allowed.Objects in a Tree 3 min read Like