📌  相关文章
📜  如何修复 TreeSet 中的Java.lang.ClassCastException?

📅  最后修改于: 2022-05-13 01:55:42.896000             🧑  作者: Mango

如何修复 TreeSet 中的Java.lang.ClassCastException?

Java中的 TreeSet 类实现了 Set 接口,该接口使用树来存储元素,这些元素包含按升序存储的唯一对象。在使用 TreeSet 对象时,您可能会遇到名为Java.lang.ClassCastException的异常。基本上, TreeSet 元素使用自然排序或使用构造函数中定义的 Comparator 进行排序。如果两者都没有发生,即自然排序没有发生并且也没有提供任何比较器,那么Java会抛出一个异常,即Java.lang.ClassCastException

例子

Java
// Java program to demonstrate ClassCastException by TreeSet
  
import java.util.TreeSet;
  
// class which is going to assign
// student marks
class Student {
  
    int marks;
  
    // constructor
    public Student(int marks) { this.marks = marks; }
  
    // override toString() method
    // for display purpose
    public String toString()
    {
        return "Student marks = " + this.marks;
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring Tree Set
        TreeSet treeSet = new TreeSet();
  
        // this line will throw java.lang.ClassCastException
        treeSet.add(new Student(1));
  
        // Displaying the contents of in treeSet
        System.out.println(treeSet);
    }
}


Java
// Java program to sort student data
// according to marks
// using Comparable interface
  
import java.util.TreeSet;
  
// class which is going to assign
// student marks
class Student implements Comparable {
  
    int id;
    String name;
    int marks;
  
    // constructor
    public Student(int id, String name, int marks)
    {
  
        // assigning values
        this.id = id;
        this.name = name;
        this.marks = marks;
    }
  
    // compareTo method to sort in
    // ascending order
    public int compareTo(Student obj)
    {
        return this.marks - obj.marks;
    }
  
    // override toString() method
    // for display purpose
    public String toString()
    {
        return "Id: " + this.id + " Name: " + this.name
            + " Marks: " + this.marks;
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring Tree Set
        TreeSet treeSet = new TreeSet();
  
        treeSet.add(new Student(1, "Suresh", 87));
        treeSet.add(new Student(2, "Ramesh", 78));
        treeSet.add(new Student(3, "Lokesh", 95));
  
        // Displaying the contents of in treeSet
        System.out.println(treeSet);
    }
}


Java
// Java program to sort student data
// according to marks using custom class
// which implements Comparator
  
// comparator interface present in
// java.util package
import java.util.*;
// class which is going to assign
// student marks
class Student {
  
    int id;
    String name;
    int marks;
  
    // constructor
    public Student(int id, String name, int marks)
    {
  
        // assigning values
        this.id = id;
        this.name = name;
        this.marks = marks;
    }
  
    // method to return
    // current marks of student
    public int getMarks() { return this.marks; }
  
    // override toString() method
    // for display purpose
    public String toString()
    {
        return "Id: " + this.id + " Name: " + this.name
            + " Marks: " + this.marks;
    }
}
  
// StuComparator class will compare
// objects ans sorts in
// ascending order
class StuComparator implements Comparator {
  
    // defining compare method
    public int compare(Student obj1, Student obj2)
    {
        return obj1.getMarks() - obj2.getMarks();
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring Tree Set
        TreeSet treeSet
            = new TreeSet(new StuComparator());
  
        treeSet.add(new Student(1, "Suresh", 87));
        treeSet.add(new Student(2, "Ramesh", 78));
        treeSet.add(new Student(3, "Lokesh", 95));
  
        // Displaying the contents of in treeSet
        System.out.println(treeSet);
    }
}


输出

我们可以通过两种方式解决这个异常:

  1. 通过实现Comparable接口
  2. 通过定义自定义 Comparator 类

方法一(实现可比接口)

Java Comparable 接口由一个类实现,该类用于根据自然顺序对对象进行比较和排序。使用 compareTo()函数可以进行自然排序。字符串对象和包装类对象根据内置的compareTo()函数进行排序。

如果compareTo函数返回正数或负数或零,则当前对象分别大于、小于和等于提供的对象。

示例 1:

Java

// Java program to sort student data
// according to marks
// using Comparable interface
  
import java.util.TreeSet;
  
// class which is going to assign
// student marks
class Student implements Comparable {
  
    int id;
    String name;
    int marks;
  
    // constructor
    public Student(int id, String name, int marks)
    {
  
        // assigning values
        this.id = id;
        this.name = name;
        this.marks = marks;
    }
  
    // compareTo method to sort in
    // ascending order
    public int compareTo(Student obj)
    {
        return this.marks - obj.marks;
    }
  
    // override toString() method
    // for display purpose
    public String toString()
    {
        return "Id: " + this.id + " Name: " + this.name
            + " Marks: " + this.marks;
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring Tree Set
        TreeSet treeSet = new TreeSet();
  
        treeSet.add(new Student(1, "Suresh", 87));
        treeSet.add(new Student(2, "Ramesh", 78));
        treeSet.add(new Student(3, "Lokesh", 95));
  
        // Displaying the contents of in treeSet
        System.out.println(treeSet);
    }
}
输出
[Id: 2 Name: Ramesh Marks: 78, Id: 1 Name: Suresh Marks: 87, Id: 3 Name: Lokesh Marks: 95]

方法 2(使用自定义比较器类)

比较器是一个必须由类实现的接口,我们可以通过它对用户定义的类的对象进行排序。它有两个被广泛使用的主要方法, compare(T o1, T o2) 和 equals(Object obj) 分别返回一个 int 和 boolean 。让我们使用比较器实现相同的示例。

Java

// Java program to sort student data
// according to marks using custom class
// which implements Comparator
  
// comparator interface present in
// java.util package
import java.util.*;
// class which is going to assign
// student marks
class Student {
  
    int id;
    String name;
    int marks;
  
    // constructor
    public Student(int id, String name, int marks)
    {
  
        // assigning values
        this.id = id;
        this.name = name;
        this.marks = marks;
    }
  
    // method to return
    // current marks of student
    public int getMarks() { return this.marks; }
  
    // override toString() method
    // for display purpose
    public String toString()
    {
        return "Id: " + this.id + " Name: " + this.name
            + " Marks: " + this.marks;
    }
}
  
// StuComparator class will compare
// objects ans sorts in
// ascending order
class StuComparator implements Comparator {
  
    // defining compare method
    public int compare(Student obj1, Student obj2)
    {
        return obj1.getMarks() - obj2.getMarks();
    }
}
  
// Driver class
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring Tree Set
        TreeSet treeSet
            = new TreeSet(new StuComparator());
  
        treeSet.add(new Student(1, "Suresh", 87));
        treeSet.add(new Student(2, "Ramesh", 78));
        treeSet.add(new Student(3, "Lokesh", 95));
  
        // Displaying the contents of in treeSet
        System.out.println(treeSet);
    }
}
输出
[Id: 2 Name: Ramesh Marks: 78, Id: 1 Name: Suresh Marks: 87, Id: 3 Name: Lokesh Marks: 95]