How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report The Java Collections Framework includes the TreeMap class, in which Java offers a sorted collection of key-value pairs. By default, TreeMap uses a custom Comparator or arranges components according to their natural ordering. In this article, we will learn how to apply a custom order for key-value pairs in a TreeMap. Sorting for Key-Value Pairs in a TreeMapWe must provide a custom Comparator during TreeMap instantiation to create a custom order for key-value pairs in a TreeMap. The items' storage order will be specified by this comparator. Program to Implement a Custom Order for Key-Value Pairs in a TreeMapAssume for the moment that we have a TreeMap with people's names as values and their ages as keys. The entries should be arranged in decreasing order according to age. Below is the Program to Implement a Custom Order for Key-Value Pairs in a TreeMap: Java // Java program to apply a custom order // For key-value pairs in a TreeMap import java.util.*; // Driver Class public class CustomOrderTreeMap { // Main Method public static void main(String[] args) { // Creating a TreeMap with a custom comparator // For descending order based on keys (ages) TreeMap<Integer, String> customOrderedMap = new TreeMap<>(Collections.reverseOrder()); // Adding key-value pairs customOrderedMap.put(25, "Rahul"); customOrderedMap.put(30, "Abhi"); customOrderedMap.put(22, "Charan"); customOrderedMap.put(35, "Gopi"); // Displaying the TreeMap System.out.println("Custom Ordered TreeMap: " + customOrderedMap); } } OutputCustom Ordered TreeMap: {35=Gopi, 30=Abhi, 25=Rahul, 22=Charan} Explanation of the Program:In the above program, we have instantiated a customOrderedMap TreeMap, we provide Collections.reverseOrder() as the Comparator. This guarantees that the items are arranged according to keys (ages) in decreasing order.We expand the TreeMap with key-value pairs.The TreeMap with its items Sorted according to the unique order specified by the Comparator is Shown in the Output. Comment More infoAdvertise with us Next Article How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? R rahulkatix28 Follow Improve Article Tags : Java Java Programs java-TreeMap Java Examples Practice Tags : Java Similar Reads How to Copy Key-Value Pairs from One TreeMap to Another in Java? In Java, a TreeMap is a Map implementation that stores key-value pairs in a red-black tree structure. It allows insertions and deletions of key-value pairs due to its tree implementation. These operations take O(log n) time on average. In this article, we will be learning how to copy key-value pairs 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 Implement a Custom Order Sorting for Elements in a LinkedHashSet? In Java, LinkedHashSet is the implementation is different from the HashSet. HashSet maintains the Doubly-LinkedList across all of its elements of the LinkedHashSet. In LinkedHashSet, it maintains to sort all elements of the object in a List. In this article, we will learn to implement a custom order 2 min read How to Implement a Cache Eviction Policy using TreeMap in Java? To enhance application speed, caching is a method that stores frequently visited data in memory. When the cache fills up, a cache eviction policy decides which things must be removed. A sorted map implementation is provided by Java's TreeMap, which may be used to create a cache with a unique evictio 3 min read How to Implement a Custom Hash function for Keys in a HashMap in Java? In Java, HashMap is the data structure that implements the Map interface. This is used to save the data in the form of key-value pairs. In this article, we will learn how to implement a Custom Hash function for keys in a HashMap in Java. In Java, implementing the custom hash function for keys in a H 2 min read How to Sort a TreeMap By Value in Java? 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 3 min read 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 Getting Least Value Element From a Set by Using Sorting Logic on TreeSet in Java In order to get the value element of the user-defined object, one needs to implement the sorting logic in TreeSet. Now in order to implement the sorting logic on the user-defined objects, the Comparator object needs to be passed along the TreeSet constructor call. Further, Comparator implementation 3 min read How to Replace a Value for the Given Key in the TreeMap? The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. It is the implementation class of Map i 4 min read How to Update the Value of an Existing Key in a LinkedHashMap in Java? In Java programming, a LinkedHahMap is like HashMap with additional features in the Java Collections framework. It keeps track of the order in which elements were added. A regular HashMap doesn't have a fixed order for elements. LinkedHashMap uses an approach with a doubly-linked list to remember th 3 min read Like