Java Program to Sort Keys in TreeMap by using Comparator with User Defined Objects
The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. To sort keys in TreeMap by using a comparator with user-defined objects in Java we have to create a class that implements the Comparator interface to override the compare method.
// AccordingMarks class that implements the
// comparator interface to override compare method
class AccordingMarks implements Comparator<Student> {
public int compare(Student s1, Student s2) {
return s1.getMarks().compareTo(s2.getMarks());
}
}
In the below code, we are passing a custom object as a key in TreeMap i.e Student user-defined class. In this case, we need to pass the comparator AccordingMarks in the constructor, while creating the TreeMap object that sorts the TreeMap according to marks of the student.
Example 1: Sort keys in ascending order of the marks
Java
import java.util.*;
class Student {
private String name;
private int marks;
public Student(String name, Integer marks)
{
this .name = name;
this .marks = marks;
}
public String getName() { return this .name; }
public Integer getMarks() { return this .marks; }
public String toString()
{
return this .name + ": " + marks;
}
}
class AccordingMarks implements Comparator<Student> {
public int compare(Student s1, Student s2)
{
return s1.getMarks().compareTo(s2.getMarks());
}
}
public class GFG {
public static void main(String[] args)
{
TreeMap<Student, Integer> map
= new TreeMap<>( new AccordingMarks());
map.put( new Student( "Akshay" , 400 ), 1 );
map.put( new Student( "Bina" , 500 ), 2 );
map.put( new Student( "Chintu" , 300 ), 3 );
System.out.println(
"TreeMap keys sorting in ascending order of the marks:" );
for (Map.Entry<Student, Integer> entry :
map.entrySet()) {
System.out.println( "Key : (" + entry.getKey()
+ "), Value : "
+ entry.getValue());
}
}
}
|
Output:
TreeMap keys sorting in ascending order of the marks:
Key : (Chintu: 300), Value : 3
Key : (Akshay: 400), Value : 1
Key : (Bina: 500), Value : 2
Example 2: Sort keys into descending order of the marks
Java
import java.util.*;
class Student {
private String name;
private int marks;
public Student(String name, Integer marks)
{
this .name = name;
this .marks = marks;
}
public String getName() { return this .name; }
public Integer getMarks() { return this .marks; }
public String toString()
{
return this .name + ": " + marks;
}
}
class AccordingMarks implements Comparator<Student> {
public int compare(Student s1, Student s2)
{
return s2.getMarks().compareTo(s1.getMarks());
}
}
public class GFG {
public static void main(String[] args)
{
TreeMap<Student, Integer> map
= new TreeMap<>( new AccordingMarks());
map.put( new Student( "Akshay" , 400 ), 1 );
map.put( new Student( "Bina" , 500 ), 2 );
map.put( new Student( "Chintu" , 300 ), 3 );
System.out.println(
"TreeMap Keys sorted in descending order of the marks: " );
for (Map.Entry<Student, Integer> entry :
map.entrySet()) {
System.out.println( "Key : (" + entry.getKey()
+ "), Value : "
+ entry.getValue());
}
}
}
|
Output:
TreeMap Keys sorted in descending order of the marks:
Key : (Bina: 500), Value : 2
Key : (Akshay: 400), Value : 1
Key : (Chintu: 300), Value : 3
Last Updated :
04 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...