Skip to content
Related Articles

Related Articles

Sort LinkedHashMap by Keys using Comparable Interface in Java
  • Last Updated : 04 Jan, 2021

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 keys using the comparable interface in Java first, we create a class that implements the comparable interface. In this class, we override the compareTo() method.

// Student class implements comparable interface

class Student implements Comparable<Student> {
    String name;

    Student(String name) {
        this.name = name;
    }

    // Override toString method
    public String toString() {
        return this.name;
    }

    // Override compareTo method
    public int compareTo(Student stu) {
        return this.name.compareTo(stu.name);
    }
}

And then we pass the LinkedHashMap to TreeMap constructor to sort.

TreeMap<Student, Integer> tree_map = new TreeMap<>(map);

Below is the full implementation of the approach:

Example 1



Java




// Java program demonstrate how to Sort LinkedHashMap by
// keys using Comparable interface
import java.util.*;
  
// Student class implements comparable interface
class Student implements Comparable<Student> {
    String name;
  
    Student(String name) { this.name = name; }
  
    // override toString method
    public String toString() { return this.name; }
  
    // Override compareTo method to sort LinkedHashMap keys
    // in accending order
    public int compareTo(Student stu)
    {
        return this.name.compareTo(stu.name);
    }
}
  
class GFG {
    public static void main(String[] args)
    {
  
        // New LinkedHashMap
        LinkedHashMap<Student, Integer> map
            = new LinkedHashMap<>();
  
        // Adding elements to the map
        map.put(new Student("Bina"), 200);
        map.put(new Student("Akshay"), 400);
        map.put(new Student("Chintu"), 500);
  
        // Print Before sort
        System.out.println(
            "Before sort keys in accending order : " + map);
  
        // TreeMap to sort LinkedHashMap using comparable
        TreeMap<Student, Integer> tree_map
            = new TreeMap<>(map);
  
        // Print after sorting
        System.out.println(
            "After sort keys in accending order : "
            + tree_map);
    }
}
Output
Before sort keys in accending order : {Bina=200, Akshay=400, Chintu=500}
After sort keys in accending order : {Akshay=400, Bina=200, Chintu=500}

Example 2

Java




// Java program demonstrate how to Sort LinkedHashMap by
// keys using Comparable interface
import java.util.*;
  
// Student class implements comparable interface
class Student implements Comparable<Student> {
    String name;
  
    Student(String name) { this.name = name; }
  
    // override toString method
    public String toString() { return this.name; }
  
    // Override compareTo method to sort LinkedHashMap keys
    // in decending order
    public int compareTo(Student stu)
    {
        return stu.name.compareTo(this.name);
    }
}
  
class GFG {
    public static void main(String[] args)
    {
  
        // New LinkedHashMap
        LinkedHashMap<Student, Integer> map
            = new LinkedHashMap<>();
  
        // Adding elements to the map
        map.put(new Student("Bina"), 200);
        map.put(new Student("Akshay"), 400);
        map.put(new Student("Chintu"), 500);
  
        // Print Before sort
        System.out.println(
            "Before sort keys in decending order : " + map);
  
        // TreeMap to sort LinkedHashMap using comparable
        TreeMap<Student, Integer> tree_map
            = new TreeMap<>(map);
  
        // Print after sorting
        System.out.println(
            "After sort keys in decending order : "
            + tree_map);
    }
}
Output
Before sort keys in decending order : {Bina=200, Akshay=400, Chintu=500}
After sort keys in decending order : {Chintu=500, Bina=200, Akshay=400}

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :