在Java中使用 TreeMap 时如何修复Java .lang.ClassCastException ?
Java.lang.ClassCastException是Java中未经检查的异常之一。当我们尝试将一种类类型的对象转换为另一种类类型的对象时,它可能会发生在我们的程序中。
当我们使用自定义类对象作为 TreeMap 中的键并且既没有实现可比较接口也没有实现比较器接口时,就会出现Java .lang.ClassCastException。
因此,在Java中使用 TreeMap 时,有两种方法可以修复Java .lang.ClassCastException :
- 使用可比
- 使用比较器
方法 1:使用Comparable
我们可以通过用作 TreeMap 键的对象实现 Comparable 接口来修复Java.lang.ClassCastException。
伪代码:
// Custom class Student implements comparable interface
class Student implements Comparable {
String name;
Integer marks;
public Student(String name, Integer marks) {
this.name = name;
this.marks = marks;
}
// Override toString method
public String toString() {
return this.name + " : " + this.marks;
}
public int getMarks() {
return this.marks;
}
// Override compareTo method that sort treemap in the ascending order of the marks
public int compareTo(Student stu) {
return this.getMarks() - stu.getMarks();
}
}
执行:
Java
// Java program to demonstrate how to fix
// java.lang.ClassCastException while using the TreeMap
import java.util.*;
// Custom class Student implements comparable interface
class Student implements Comparable {
String name;
Integer marks;
public Student(String name, Integer marks)
{
this.name = name;
this.marks = marks;
}
// Override toString method
public String toString()
{
return this.name + " : " + this.marks;
}
public int getMarks() { return this.marks; }
// Override compareTo method that sort treemap in the
// ascending order of the marks
public int compareTo(Student stu)
{
return this.getMarks() - stu.getMarks();
}
}
public class GFG {
public static void main(String[] args)
{
// New TreeMap
TreeMap map = new TreeMap<>();
map.put(new Student("Akshay", 500), 1);
map.put(new Student("Bhanu", 600), 2);
map.put(new Student("Chetan", 300), 3);
System.out.println("The Treemap : " + map);
}
}
Java
// Java program to demonstrate how to fix
// java.lang.ClassCastException while using the TreeMap
import java.util.*;
// Custom class Student implements comparable interface
class Student {
String name;
Integer marks;
public Student(String name, Integer marks)
{
this.name = name;
this.marks = marks;
}
// Override toString method
public String toString()
{
return this.name + " : " + this.marks;
}
public int getMarks() { return this.marks; }
}
// Custom comparator
class MyComparator implements Comparator {
// Compare method that sort TreeMap in the ascending
// order of the marks
public int compare(Student stu1, Student stu2)
{
return stu1.getMarks() - stu2.getMarks();
}
}
public class GFG {
public static void main(String[] args)
{
// New TreeMap
TreeMap map
= new TreeMap<>(new MyComparator());
map.put(new Student("Akshay", 500), 1);
map.put(new Student("Bhanu", 600), 2);
map.put(new Student("Chetan", 300), 3);
System.out.println("The Treemap : " + map);
}
}
输出
The Treemap : {Chetan : 300=3, Akshay : 500=1, Bhanu : 600=2}
方法 2:使用比较器
我们可以通过在创建 TreeMap 时为构造函数提供自定义比较器来修复Java.lang.ClassCastException。
伪代码:
// Custom comparator
class MyComparator implements Comparator {
// Compare method that sort TreeMap in the ascending order of the marks
public int compare(Student stu1, Student stu2) {
return stu1.getMarks() - stu2.getMarks();
}
}
执行:
Java
// Java program to demonstrate how to fix
// java.lang.ClassCastException while using the TreeMap
import java.util.*;
// Custom class Student implements comparable interface
class Student {
String name;
Integer marks;
public Student(String name, Integer marks)
{
this.name = name;
this.marks = marks;
}
// Override toString method
public String toString()
{
return this.name + " : " + this.marks;
}
public int getMarks() { return this.marks; }
}
// Custom comparator
class MyComparator implements Comparator {
// Compare method that sort TreeMap in the ascending
// order of the marks
public int compare(Student stu1, Student stu2)
{
return stu1.getMarks() - stu2.getMarks();
}
}
public class GFG {
public static void main(String[] args)
{
// New TreeMap
TreeMap map
= new TreeMap<>(new MyComparator());
map.put(new Student("Akshay", 500), 1);
map.put(new Student("Bhanu", 600), 2);
map.put(new Student("Chetan", 300), 3);
System.out.println("The Treemap : " + map);
}
}
输出
The Treemap : {Chetan : 300=3, Akshay : 500=1, Bhanu : 600=2}