Java Program to Sort LinkedHashMap By Values Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 can be accessed in their insertion order. Hence LinkedHashMap Is the child class of HashMap. Now, in LinkedHashMap insertion order has to be maintained, so convert the LinkedHashMap into a list and after that print the list in which values are in sorted order. Illustration: Input : LinkedHashMap = {{“for”, 2}, {“Geek”, 3}, {“Geeks”, 1}} Output: Key -> Geeks : value -> 1 Key -> for : value -> 2 Key -> Geek : value ->3 Procedure: Create an object of LinkedHashMap Class where the object is declared of type Integer and String.Add elements to the above object created of the map using the put() method. Elements here are key-value pairs.Retrieve above all entries from the map and convert them to a list using entrySet() method.Sort the value of the list using the custom comparator.Now use the Collections class sort method inside which we are using a custom comparator to compare the value of a map.Print the above list object using for each loop. Implementation: Example Java // java Program to Sort LinkedHashMap by Values // Importing all classes // from java.util package import java.util.*; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; // Main Class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of LinkedHashMap Class // Declaring object of Integer and String type LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); // Adding elements to the object of Map // using the put() method // Elements here are key-value pairs map.put("for", 2); map.put("Geek", 3); map.put("Geeks", 1); // Now, getting all entries from map and // convert it to a list using entrySet() method List<Map.Entry<String, Integer> > list = new ArrayList<Map.Entry<String, Integer> >( map.entrySet()); // Using collections class sort method // and inside which we are using // custom comparator to compare value of map Collections.sort( list, new Comparator<Map.Entry<String, Integer> >() { // Comparing two entries by value public int compare( Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) { // Subtracting the entries return entry1.getValue() - entry2.getValue(); } }); // Iterating over the sorted map // using the for each method for (Map.Entry<String, Integer> l : list) { // Printing the sorted map // using getKey() and getValue() methods System.out.println("Key ->" + " " + l.getKey() + ": Value ->" + l.getValue()); } } } OutputKey -> Geeks: Value ->1 Key -> for: Value ->2 Key -> Geek: Value ->3 Comment More info M mroshanmishra0072 Follow Improve Article Tags : Java Java Programs Java-Collections Java-LinkedHashMap Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like