Sort LinkedHashMap by Keys in Java Last Updated : 11 Dec, 2020 Comments Improve Suggest changes Like Article Like Report LinkedHashMap maintains insertion order. Convert LinkedHashMap into TreeMap and after that print keys of TreeMap which are sorted in nature. Example: Input: linkedHashMap = {{5,4}, {3,44}, {4,15}, {1,20}, {2,11}} Output: key -> 1 : value -> 20 key -> 2 : value -> 11 key -> 3 : value -> 44 key -> 4 : value -> 15 key -> 5: value -> 4 Approach: Take LinkedHashMap as an input.Create new TreeMap.Pass LinkedHashMap object into the constructor of TreeMap.Print Keys of TreeMap object. Below is the implementation of the above approach: Java // Sort LinkedHashMap by keys in Java import java.util.*; import java.io.*; class GFG { public static void main(String[] args) { LinkedHashMap<Integer, Integer> lMap = new LinkedHashMap<>(); // adding key-value pairs to LinkedHashMap object lMap.put(5, 4); lMap.put(3, 44); lMap.put(4, 15); lMap.put(1, 20); lMap.put(2, 11); System.out.println("After Sorting :\n"); // convert to TreeMap Map<Integer, Integer> map = new TreeMap<>(lMap); // iterate acc to ascending order of keys for (Integer sKey : map.keySet()) { System.out.println("Key -> " + sKey + ": Value -> " + lMap.get(sKey)); } } } OutputAfter Sorting : Key -> 1: Value -> 20 Key -> 2: Value -> 11 Key -> 3: Value -> 44 Key -> 4: Value -> 15 Key -> 5: Value -> 4 Comment More infoAdvertise with us Next Article Sort LinkedHashMap by Keys in Java K kaaruni1124 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-LinkedHashMap +1 More Practice Tags : Java Similar Reads How to Print all Keys of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains a key and its respective value. Unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to print all the Keys present in our LinkedHashMap in java. We have to iterate through each Key in our LinkedHas 2 min read Sort LinkedHashMap by Keys using Comparable Interface in Java The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. To sort LinkedHashMap by key 3 min read How to Add Key-Value pairs to LinkedHashMap in Java? LinkedHashMap is a Hash table and linked list implementation of the Map interface. In LinkedHashMap order of key-value pair depends on the order in which keys were inserted into the map. Insertion order does not affect if a key is reinserted into the map. Example: Input: Key: 1 Value : 1221 Key: 2 V 2 min read How to iterate LinkedHashMap in Java? LinkedHashMap class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted. Th 2 min read Java Program to Implement LinkedHashMap API The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements 3 min read Like