A LinkedHashMap maintains the insertion order of its entries, but sometimes we need to arrange its keys in ascending or descending order. Java allows us to sort a LinkedHashMap by keys by using a TreeMap along with the Comparable interface.
- The Comparable interface defines the natural ordering of the keys through the compareTo() method.
- A TreeMap automatically arranges its entries according to the ordering defined by its keys.
Using Comparable Interface with TreeMap
Implement Comparable in the key class and override the compareTo() method to define ascending or descending order. Then, pass the LinkedHashMap to a TreeMap.
Approach:
- Create a Student class that implements Comparable<Student>.
- Override the compareTo() method to compare the name field.
- Then, pass the LinkedHashMap to a TreeMap, which sorts the entries according to the natural ordering defined by compareTo().
Syntax:
TreeMap<Student, Integer> treeMap = new TreeMap<>(map);
1: Sort Keys in Ascending Order
In this example, the compareTo() method compares student names in their natural alphabetical order.
import java.util.*;
class Student implements Comparable<Student> {
String name;
Student(String name) { this.name = name; }
@Override public String toString() { return name; }
@Override public int compareTo(Student student)
{
return this.name.compareTo(student.name);
}
}
class GFG {
public static void main(String[] args)
{
LinkedHashMap<Student, Integer> map
= new LinkedHashMap<>();
map.put(new Student("Bina"), 200);
map.put(new Student("Akshat"), 400);
map.put(new Student("Chintu"), 500);
System.out.println("Before sorting: " + map);
TreeMap<Student, Integer> treeMap
= new TreeMap<>(map);
System.out.println(
"After sorting in ascending order: " + treeMap);
}
}
Output
Before sorting: {Bina=200, Akshat=400, Chintu=500}
After sorting in ascending order: {Akshat=400, Bina=200, Chintu=500}
Explanation: The Student class implements Comparable<Student> and compares student names using compareTo(). When the LinkedHashMap is passed to the TreeMap, the keys are arranged in ascending alphabetical order.
2: Sort Keys in Descending Order
To sort the keys in descending order, reverse the comparison inside the compareTo() method.
import java.util.*;
class Student implements Comparable<Student> {
String name;
Student(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public int compareTo(Student student) {
return student.name.compareTo(this.name);
}
}
class GFG {
public static void main(String[] args) {
LinkedHashMap<Student, Integer> map = new LinkedHashMap<>();
map.put(new Student("Bina"), 200);
map.put(new Student("Akshat"), 400);
map.put(new Student("Chintu"), 500);
System.out.println("Before sorting: " + map);
TreeMap<Student, Integer> treeMap = new TreeMap<>(map);
System.out.println("After sorting in descending order: "
+ treeMap);
}
}
Output
Before sorting: {Bina=200, Akshat=400, Chintu=500}
After sorting in descending order: {Chintu=500, Bina=200, Akshat=400}
Explanation: Here, compareTo() compares the student names in reverse order by calling student.name.compareTo(this.name). Therefore, the TreeMap stores the entries with keys arranged in descending alphabetical order.
Note: The keys are sorted in descending alphabetical order, while the associated values remain unchanged.