Java Program to Sort LinkedHashMap By Values Last Updated : 08 Sep, 2022 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 infoAdvertise with us Next Article Java Program to Sort LinkedHashMap By Values M mroshanmishra0072 Follow Improve Article Tags : Java Java Programs Java-Collections Java-LinkedHashMap Practice Tags : JavaJava-Collections Similar Reads Java Program to Sort a HashMap by Keys and Values HashMap<K, V> is a Java Collection and is a part of java.util package. It provides the basic implementation of the Map interface of Java. It stores the data in the form of Key, Value pairs, where the keys must be unique but there is no restriction for values. If we try to insert the duplicate 3 min read Java Program to Sort LinkedList using Comparable In Java, LinkedList is a part of the collection framework provided in java.util package. LinkedList is a linear data structure where all the elements are unsorted in contiguous memory locations. The advantage of LinkedList is it is dynamic and easy to insert and delete any element. We can not access 5 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 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 Sort LinkedHashMap by Keys in Java 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 - 2 min read How to Get All the Values of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved. The task is to get all the values present in our LinkedHashMap that is linked with their respective key. Use Iteration or pred 4 min read How to Merge Two LinkedHashMaps in Java? In Java, LinkedHashMap is a class that extends HashMap and maintains the order of elements based on the order of insertion. Merging two LinkedHashMaps involves combining their key-value pairs while ensuring that the order is preserved. In, this article we will explore different approaches to merging 3 min read Sort LinkedHashMap by Values using Comparable Interface in Java The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. Assuming you have gone through LinkedHashMap in java and know about LinkedHashMap. Syntax: int compare(T obj) ; Illustration: Input : { GEEKS=1, geeks=3, for=2 } Output : { GEEKS=1 2 min read How to Convert all LinkedHashMap Values to a List in Java? The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List 2 min read Java Program to Implement LinkedHashSet API The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained, this class is used. When iterating through a HashSet, the order is unpredictable, while a LinkedHashSet lets us iterate through the element 4 min read Like