Open In App

Sort LinkedHashMap by Values using Comparable Interface in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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, for=2, geeks=3 }

Input  : { 101 = 2, 102 = 9, 103 = 1, 105 = 7 }
Output : { 103 = 1, 101 = 2, 105 = 7, 102 = 9 }

Method:  While using Comparable interface to sort LinkedHashMap by value, this interface imposes a total ordering on the objects of each class that implements it. This ordering refers to as the class’s natural ordering and the class’s comparator  method is referred to as its natural comparison method.

Implementation:

Example 

Java




// Java program to sort the values of LinkedHashMap
  
// Importing required classes from
// java.util package
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.*;
  
// Class 
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an LinkedHashMap object
        LinkedHashMap<String, Integer> l_map
            = new LinkedHashMap<String, Integer>();
  
        // Adding element to LinkedHashSet
        // Custom inputs
        l_map.put("Computer", 1);
        l_map.put("Science", 3);
        l_map.put("Portal", 2);
  
        // Display message
        System.out.print(
            "LinkedHashMap without sorting : ");
  
        // Print the elements of Map in above object
        // just after addition without sorting
        System.out.println(l_map);
  
        // Convert key-value from the LinkedHashMap to List
        // using entryset() method
        List<Map.Entry<String, Integer> > list
            = new ArrayList<Map.Entry<String, Integer> >(
                l_map.entrySet());
  
        // Comparable Interface function to
        // sort the values of List
        Collections.sort(
            list,
            new Comparator<Map.Entry<String, Integer> >() {
                // Comparing entries
                public int compare(
                    Entry<String, Integer> entry1,
                    Entry<String, Integer> entry2)
                {
                    return entry1.getValue()
                        - entry2.getValue();
                }
            });
  
        // Clear the above LinkedHashMap
        // using clear() method
        l_map.clear();
  
        // Iterating over elements using for each loop
        for (Map.Entry<String, Integer> entry : list) {
  
            // Put all sorted value back to the
            // LinkedHashMap
            l_map.put(entry.getKey(), entry.getValue());
        }
  
        // Display and print
        // the sorted value of LinkedHashMap
        System.out.println(
            "LinkedHashMap after sorting   : " + l_map);
    }
}


Output

LinkedHashMap without sorting : {Computer=1, Science=3, Portal=2}
LinkedHashMap after sorting   : {Computer=1, Portal=2, Science=3}


Last Updated : 13 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads