
TreeMap是Java Collection Framework的一个类,实现了NavigableMap接口。它将地图的元素存储在树结构中,并提供了一种有效的替代方法,以按排序顺序存储键值对。请注意,在创建TreeMap对象时,必须使用Comparable接口或Comparator接口之一,以便我们可以维护其元素的排序顺序,否则,我们将遇到java.lang.ClassCastException。在本文中,我们将解释如何使用Comparable和Comparator接口来解决TreeMap中的ClassCastException问题
修复TreeMap中的java.lang.ClassCastException问题
让我们从一个示例程序开始讨论,它将向我们展示TreeMap中的ClassCastException。
Example 1
的中文翻译为:示例 1
在下面的示例中,我们将尝试向TreeMap中添加自定义类对象,而不使用Comparable和Comparator接口,以展示Java编译器抛出java.lang.ClassCastException的情况。
import java.util.*;
public class TrMap {
String item;
int price;
TrMap(int price, String item) {
// this keyword shows these variables belong to constructor
this.item = item;
this.price = price;
}
// method for converting object into string
public String toString() {
return "Item: " + item + ", " + "Price: " + price;
}
public static void main(String[] args) {
// Declaring collection TreeMap
TreeMap obj = new TreeMap<>();
// Adding object to the obj map
obj.put(new TrMap(495, "TShirt"), 1);
obj.put(new TrMap(660, "Shirt"), 2);
// printing details obj map
System.out.println("Elements of the map: " + obj);
}
}
Output
的中文翻译为:输出
Exception in thread "main" java.lang.ClassCastException: class TrMap cannot be cast to class java.lang.Comparable (TrMap is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap') at java.base/java.util.TreeMap.compare(TreeMap.java:1569) at java.base/java.util.TreeMap.addEntryToEmptyMap(TreeMap.java:776) at java.base/java.util.TreeMap.put(TreeMap.java:785) at java.base/java.util.TreeMap.put(TreeMap.java:534) at TrMap.main(TrMap.java:18)
如何使用Comparable接口修复java.lang.ClassCastException错误
让我们通过介绍Comparable接口来开始讨论
Comparable接口
当我们想按照自然顺序对自定义对象进行排序时,这个接口非常有用。例如,它按照字典顺序对字符串进行排序,按照数字顺序对数字进行排序。这个接口在'java.lang'包中可用。通常,此包中定义的类和接口默认可供我们使用,因此不需要显式导入此包。
语法
class nameOfclass implements Comparable
在这里,class是创建一个类的关键字,而implements则是启用接口提供的特性的关键字。
立即学习“Java免费学习笔记(深入)”;
compareTo() 的翻译为:
The Comparable interface defines only a single method named 'CompareTo' that can be overridden in order to sort the collection of objects. It gives the power to compare the objects of a class to itself. It returns 0 when 'this' object is equal to the passed object, a positive value if 'this' object is greater otherwise a negative value.
语法
compareTo(nameOfclass nameOfobject);
Example 2
的中文翻译为:示例2
以下示例演示了在修复ClassCastException中使用Comparable的用法。
方法
创建一个实现Comparable接口的类'TrMap'。在类内部,声明两个变量,并定义一个带有两个参数的构造函数,参数类型分别为字符串和双精度的'item'和'price'。
进一步移动,我们将使用'toString()'方法将对象的数据转换为字符串。然后,定义'compareTo'方法,并将一个类'TrMap'的对象作为参数,用于比较'this'对象和新创建的对象
现在,在main()方法中,声明一个名为'obj'的TreeMap类的对象,并使用名为'put()'的内置方法将对象的详细信息存储到其中。'item'是键,其对应的值是'price'。
保君发企业网站系统1.0下载保君发免费网站系统使用说明:一、 本程序完全免费,并且,保证功能全部可以使用,且无后门及木马等,请放心使用。二、 如果发现问题,请及时联系我们,我们会义务尽力解决所反映的问题。或到本公司网站下载更新程序。三、 修改三个文件就能成为自己的网站:1、顶部图片LOGO.GIF,2、替换透明动画:LOGO.SWF,3、修改#sys123.asp中的内容为你想要的内容。
最后,在for-each循环中使用'keySet()'方法来检索并打印与键相关联的值。
import java.util.*; import java.lang.*; public class TrMap implements Comparable{ String item; int price; TrMap(String item, int price) { // this keyword shows these variables belong to constructor this.item = item; this.price = price; } // method for converting object into string public String toString() { return "Item: " + item + ", " + "Price: " + price; } public String getName() { return this.item; } // overriding method public int compareTo(TrMap comp) { return this.item.compareTo(comp.item); } public static void main(String[] args) { // Declaring collection TreeMap TreeMap obj = new TreeMap<>(); // Adding object to the obj map TrMap obj1 = new TrMap("TShirt", 495); obj.put(obj1.getName(), obj1); TrMap obj2 = new TrMap("Shirt", 660); obj.put(obj2.getName(), obj2); TrMap obj3 = new TrMap("Kurti", 455); obj.put(obj3.getName(), obj3); // printing details obj map System.out.println("Elements of the map: "); for (String unKey : obj.keySet()) { System.out.println(obj.get(unKey)); } } }
Output
的中文翻译为:输出
Elements of the map: Item: Kurti, Price: 455 Item: Shirt, Price: 660 Item: TShirt, Price: 495
如何使用Comparator修复java.lang.ClassCastException问题
首先,让我们介绍Comparator接口。
比较器
正如其名称所示,它用于比较某些东西。在Java中,Comparator是一个接口,用于对自定义对象进行排序。我们可以在其内置的名为'compare()'的方法中编写自己的逻辑来对指定的对象进行排序。该方法接受两个对象作为参数,然后返回一个整数值。通过这个整数值,Comparator决定哪个对象更大
语法
class nameOfComparator implements Comparator< TypeOfComparator >() {
compare( type object1, type object2 ) {
// logic for comparison
}
}
Example 3
的中文翻译为:示例 3
下面的示例演示了在修复ClassCastException时使用Comparator的方法。
方法
首先,导入 'java.util' 包,以便我们可以使用 TreeSet
创建一个名为 'TrMap' 的类。在类内部,声明两个变量,并定义一个构造函数,该构造函数有两个参数 'item' 和 'price',分别是字符串类型和整数类型。
进一步移动,我们将使用 'toString()' 方法将对象的数据转换为字符串
然后,定义另一个实现Comparator接口的类'Comp',在其中使用'compare()'方法对TreeMap进行升序排序。
在 'main()' 方法中,通过传递 'Comp' 类的实例来创建一个 TreeMap 集合,以便进行排序
最后,使用'put()'方法将一些元素存储到TreeMap集合中,然后打印结果。
import java.util.*;
class TrMap {
String item;
int price;
TrMap(int price, String item) {
// this keyword shows these variables belong to constructor
this.item = item;
this.price = price;
}
// method for converting object into string
public String toString() {
return "Item: " + item + ", " + "Price: " + price;
}
public String getName() {
return this.item;
}
}
// use of comparator interface
class Comp implements Comparator {
// logic to sort
public int compare(TrMap i, TrMap j) {
if(i.price > j.price) {
return 1;
} else {
return -1;
}
}
}
public class Example2 {
public static void main(String[] args) {
// Declaring collection TreeMap
TreeMap obj = new TreeMap<>(new Comp());
// Adding object to the obj map
obj.put(new TrMap(495, "TShirt"), 1);
obj.put(new TrMap(660, "Shirt"), 2);
// printing details obj map
System.out.println("Elements of the map: " + obj);
}
}
Output
的中文翻译为:输出
Elements of the map: {Item: TShirt, Price: 495=1, Item: Shirt, Price: 660=2}
结论
在本文中,我们首先定义了TreeMap类,然后介绍了TreeMap中的ClassCastException。在下一部分中,我们讨论了可以帮助解决这个ClassCastException的Comparator和Comparable接口。然后,我们看到了三个示例程序,展示了ClassCastException以及如何修复这个异常。










