如何在Java中使用自定义比较器修复 TreeSet 中的Java .lang.ClassCastException ?
此错误由TreeSet引发,因为 TreeSet 用于按排序顺序存储元素,如果无法将指定元素与集合中当前的元素进行比较,则 TreeSet 将无法按排序顺序存储元素,因此 TreeSet 抛出类投异常。
如何修复此错误?
- 通过在 TreeSet 构造函数中提供自定义比较器。
Integer、Double 等包装类中已经实现了 Comparator 接口,所以我们不必担心。但是每当我们在 TreeSet 中使用用户定义的类时,我们必须执行上述任何一种方法。
方法:通过在 TreeSet 构造函数中提供自定义 Comparator
与 Comparable 不同,Comparator 是我们要比较的元素类型的外部。这是一个单独的类。我们创建了多个单独的类(实现 Comparator)以通过不同的成员进行比较。
- 创建一个实现 Comparator 的类(以及执行之前由 compareTo() 完成的工作的 compare() 方法)。
- 创建 Comparator 类的实例。
- 调用重载的 sort() 方法,为其提供列表和实现 Comparator 的类的实例。
Java
// Java program to fix java.lang.ClassCastException
// in TreeSet using Comparator
import java.util.Comparator;
import java.util.TreeSet;
class friendsMarks {
// class field
String name;
String nickName;
int marks;
// parameterised constructor
public friendsMarks(String name, String nickName,
int marks)
{
this.name = name;
this.nickName = nickName;
this.marks = marks;
}
// getter for name
public String getName() { return name; }
// setter for name
public void setName(String name) { this.name = name; }
// getter for marks
public int getMarks() { return marks; }
// setter for marks
public void setMarks(int marks) { this.marks = marks; }
// getter for nickname
public String getNickName() { return nickName; }
// setter for nickname
public void setNickName(String nickName)
{
this.nickName = nickName;
}
//@Override toString method
public String toString()
{
return "friendsMarks{"
+ "name='" + name + '\'' + ", nickName='"
+ nickName + '\'' + ", marks=" + marks + '}';
}
}
// for comparing names
class nameCompare implements Comparator {
@Override
public int compare(friendsMarks friend1,
friendsMarks friend2)
{
return friend1.getName().compareTo(
friend2.getName());
}
}
// for comparing marks
class marksCompare implements Comparator {
@Override
public int compare(friendsMarks f1, friendsMarks f2)
{
if (f1.getMarks() > f2.getMarks()) {
return 1;
}
else {
return -1;
}
}
}
// for comparing nick name
class nickNameCompare implements Comparator {
@Override
public int compare(friendsMarks obj1, friendsMarks obj2)
{
return obj1.getNickName().compareTo(
obj2.getNickName());
}
}
public class Main {
public static void main(String[] args)
{
// Creating TreeSet
// and we have to pass Comparator object
// of marksCompare class
// in the TreeSet constructor
// so that we can sort according to the marks
TreeSet treeSet
= new TreeSet<>(new marksCompare());
// adding elements to TreeSet
treeSet.add(new friendsMarks("Raushan", "Chamgader", 99));
treeSet.add(new friendsMarks("Yashdeep", "Dopa", 95));
treeSet.add(new friendsMarks("Rupesh", "Gian", 92));
treeSet.add(new friendsMarks("Shivam", "Gorilla", 47));
treeSet.add(new friendsMarks("Sarthak", "Nagin", 78));
treeSet.add(new friendsMarks("Sonika", "Chipkali", 67));
treeSet.add(new friendsMarks("Himanshu", "Lalten", 57));
System.out.println("Sorting on the basis of marks");
// Displaying using loop
for (friendsMarks tree : treeSet)
System.out.println(tree);
// Creating TreeSet
// and we have to pass Comparator object
// of nameCompare class
// in the TreeSet constructor
// so that we can sort according to the name
TreeSet treeSet1
= new TreeSet<>(new nameCompare());
// adding elements to TreeSet
treeSet1.add(new friendsMarks("Raushan", "Chamgader", 99));
treeSet1.add(new friendsMarks("Yashdeep", "Dopa", 95));
treeSet1.add(new friendsMarks("Rupesh", "Gian", 92));
treeSet1.add(new friendsMarks("Shivam", "Gorilla", 47));
treeSet1.add(new friendsMarks("Sarthak", "Nagin", 78));
treeSet1.add(new friendsMarks("Sonika", "Chipkali", 67));
treeSet1.add(new friendsMarks("Himanshu", "Lalten", 57));
// Displaying using loop
System.out.println("\n\nSorting on the basis of name");
for (friendsMarks tree : treeSet1)
System.out.println(tree);
// Creating TreeSet
// and we have to pass Comparator object
// of nickNameCompare class
// in the TreeSet constructor
// so that we can sort according to the nickname
TreeSet treeSet2
= new TreeSet<>(new nickNameCompare());
// adding elements to TreeSet
treeSet2.add(new friendsMarks("Raushan", "Chamgader", 99));
treeSet2.add(new friendsMarks("Yashdeep", "Dopa", 95));
treeSet2.add(new friendsMarks("Rupesh", "Gian", 92));
treeSet2.add(new friendsMarks("Shivam", "Gorilla", 47));
treeSet2.add(new friendsMarks("Sarthak", "Nagin", 78));
treeSet2.add(new friendsMarks("Sonika", "Chipkali", 67));
treeSet2.add(new friendsMarks("Himanshu", "Lalten", 57));
// Displaying using loop
System.out.println("\n\nSorting on the basis of nick-name");
for (friendsMarks tree : treeSet2)
System.out.println(tree);
}
}
输出
Sorting on the basis of marks
friendsMarks{name='Shivam', nickName='Gorilla', marks=47}
friendsMarks{name='Himanshu', nickName='Lalten', marks=57}
friendsMarks{name='Sonika', nickName='Chipkali', marks=67}
friendsMarks{name='Sarthak', nickName='Nagin', marks=78}
friendsMarks{name='Rupesh', nickName='Gian', marks=92}
friendsMarks{name='Yashdeep', nickName='Dopa', marks=95}
friendsMarks{name='Raushan', nickName='Chamgader', marks=99}
Sorting on the basis of name
friendsMarks{name='Himanshu', nickName='Lalten', marks=57}
friendsMarks{name='Raushan', nickName='Chamgader', marks=99}
friendsMarks{name='Rupesh', nickName='Gian', marks=92}
friendsMarks{name='Sarthak', nickName='Nagin', marks=78}
friendsMarks{name='Shivam', nickName='Gorilla', marks=47}
friendsMarks{name='Sonika', nickName='Chipkali', marks=67}
friendsMarks{name='Yashdeep', nickName='Dopa', marks=95}
Sorting on the basis of nick-name
friendsMarks{name='Raushan', nickName='Chamgader', marks=99}
friendsMarks{name='Sonika', nickName='Chipkali', marks=67}
friendsMarks{name='Yashdeep', nickName='Dopa', marks=95}
friendsMarks{name='Rupesh', nickName='Gian', marks=92}
friendsMarks{name='Shivam', nickName='Gorilla', marks=47}
friendsMarks{name='Himanshu', nickName='Lalten', marks=57}
friendsMarks{name='Sarthak', nickName='Nagin', marks=78}