使用带有用户定义对象的比较器对 TreeMap 中的键进行排序的Java程序
Java中的TreeMap用于实现 Map 接口和 NavigableMap 以及 AbstractMap 类。映射根据其键的自然顺序排序,或按 比较器 在地图创建时提供,具体取决于使用的构造函数。要在Java中使用带有用户定义对象的比较器对 TreeMap 中的键进行排序,我们必须创建一个实现 Comparator 接口的类来覆盖 compare 方法。
// AccordingMarks class that implements the
// comparator interface to override compare method
class AccordingMarks implements Comparator {
public int compare(Student s1, Student s2) {
return s1.getMarks().compareTo(s2.getMarks());
}
}
在下面的代码中,我们传递一个自定义对象作为 TreeMap 中的键,即 Student 用户定义类。在这种情况下,我们需要在构造函数中传递比较器根据标记,同时创建根据学生的分数对 TreeMap 进行排序的 TreeMap 对象。
示例 1:按标记的升序对键进行排序
Java
// Java program to demonstrate how to sort TreeMap of custom
// class objects
import java.util.*;
// Custom class
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; }
// override toString method
public String toString()
{
return this.name + ": " + marks;
}
}
// Comparator that sort elements according to marks in
// Ascending order
class AccordingMarks implements Comparator {
public int compare(Student s1, Student s2)
{
return s1.getMarks().compareTo(s2.getMarks());
}
}
// Driver Code
public class GFG {
public static void main(String[] args)
{
// New TreeMap of custom class Student
TreeMap map
= new TreeMap<>(new AccordingMarks());
// Add elements to TreeMap
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:");
// Print map using Entry
for (Map.Entry entry :
map.entrySet()) {
System.out.println("Key : (" + entry.getKey()
+ "), Value : "
+ entry.getValue());
}
}
}
Java
// Java program to demonstrate how to sort TreeMap of custom
// class objects
import java.util.*;
// Custom class
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; }
// override toString method
public String toString()
{
return this.name + ": " + marks;
}
}
// Comparator that sort elements according to marks in
// Descending order
class AccordingMarks implements Comparator {
public int compare(Student s1, Student s2)
{
return s2.getMarks().compareTo(s1.getMarks());
}
}
// Driver Code
public class GFG {
public static void main(String[] args)
{
// New TreeMap of custom class Student
TreeMap map
= new TreeMap<>(new AccordingMarks());
// Add elements to TreeMap
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: ");
// Print map using Entry
for (Map.Entry entry :
map.entrySet()) {
System.out.println("Key : (" + entry.getKey()
+ "), Value : "
+ entry.getValue());
}
}
}
输出:
TreeMap keys sorting in ascending order of the marks:
Key : (Chintu: 300), Value : 3
Key : (Akshay: 400), Value : 1
Key : (Bina: 500), Value : 2
示例 2:将键按标记的降序排序
Java
// Java program to demonstrate how to sort TreeMap of custom
// class objects
import java.util.*;
// Custom class
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; }
// override toString method
public String toString()
{
return this.name + ": " + marks;
}
}
// Comparator that sort elements according to marks in
// Descending order
class AccordingMarks implements Comparator {
public int compare(Student s1, Student s2)
{
return s2.getMarks().compareTo(s1.getMarks());
}
}
// Driver Code
public class GFG {
public static void main(String[] args)
{
// New TreeMap of custom class Student
TreeMap map
= new TreeMap<>(new AccordingMarks());
// Add elements to TreeMap
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: ");
// Print map using Entry
for (Map.Entry entry :
map.entrySet()) {
System.out.println("Key : (" + entry.getKey()
+ "), Value : "
+ entry.getValue());
}
}
}
输出:
TreeMap Keys sorted in descending order of the marks:
Key : (Bina: 500), Value : 2
Key : (Akshay: 400), Value : 1
Key : (Chintu: 300), Value : 3