cannot be cast to java.lang.Comparable
Exception in thread "main" java.lang.ClassCastException: com.myradio.People cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1294)
at java.util.TreeMap.put(TreeMap.java:538)
at java.util.TreeSet.add(TreeSet.java:255)
at com.myradio.TreeSetDemo.methodTreeSet(TreeSetDemo.java:18)
at com.myradio.TreeSetDemo.main(TreeSetDemo.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
出现这个问题的原因:
TreeSet中的元素必须是Comparable类型。 字符串和包装类在默认情况下是可比较的。 要在TreeSet中添加用户定义的对象,该对象需要实现Comparable接口。
TreeSet的应用例子
import android.support.annotation.NonNull; import java.util.Iterator;
import java.util.TreeSet; /**
* 1. order by people age
* 2. If both People object have the name and age as the same person
*/ public class TreeSetDemo {
public static void main(String [] args){
methodTreeSet();
}
private static void methodTreeSet(){
TreeSet ts = new TreeSet();
ts.add(new People("wyy",20));
ts.add(new People("cl",30));
ts.add(new People("cl",30));
ts.add(new People("cbh",10));
Iterator it = ts.iterator();
while (it.hasNext()){
People p = (People) it.next();
System.out.println(p.getName() + "..." + p.getAge());
}
}
}
class People implements Comparable{
String name;
int age;
People(String name, int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
@Override
public int compareTo(@NonNull Object o) {
if(!(o instanceof People)){
throw new RuntimeException("Not People object");
}
People p = (People) o;
if(this.age > p.getAge())
return 1;
if(this.age == p.getAge()) //if just this one condition, when the age is the same ,it will be treated as one people
return this.name.compareTo(p.getName()); //So compare name are needed.
return -1;
}
}
运行结果:
cbh...10
wyy...20
cl...30
在该对象的compareTo方法中,判断年龄相等的条件时,若直接返回0,如果年龄相同,名字不同,仍然会当作一个人看待
所以需要用次条件进行判断。
cannot be cast to java.lang.Comparable的更多相关文章
- Cannot be cast to java.lang.Comparable异常
Set集合中的treeSet问题:cannot be cast to java.lang.Comparable: 原理: Set不保存重复的元素,与Collection类似,只是行为不同,Set是基于 ...
- Set集合中的treeSet问题:cannot be cast to java.lang.Comparable;
使用TreeSet保存自定义对象时, 必须让定义对象的类实现Comparable接口,并重写compareTo()方法 否则报 实体类User:cannot be cast to java.lang. ...
- TreeMap cannot be cast to java.lang.Comparable
/** * Constructs a new, empty tree map, using the natural ordering of its * keys. All keys inserted ...
- java.lang.Long cannot be cast to java.lang.Integer解决办法
情景: mybatis连接oracle 报错: 测试增的时候,报错 Java.lang.Long cannot be cast to java.lang.Integer:删改没有报错. 排查过程: ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- java.lang.Comparable接口
转自:http://blog.csdn.net/zccst/article/details/5092920 java.lang.Comparable 接口 作者: zccst java.lang.Co ...
- 利用泛型抽取Dao层,加事务注解问题(java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType)
想利用泛型抽取BaseDao层,简化操作时出现故障: @Transactional这个注解是能够继承的.于是就想写在抽取的BaseDao层上,让实现的类能够不用写@Transactional,就可开启 ...
- Mybatis的失误填坑-java.lang.Integer cannot be cast to java.lang.String
Mybatis的CRUD小Demo 为方便查看每次的增删改结果,封装了查询,用来显示数据库的记录: public static void showInfo(){ SqlSession session ...
- Java.lang.Comparable接口和Java.util.Comparator接口的区别
Java的Comparator和Comparable当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序. 1.Com ...
随机推荐
- 神奇的namespace使用
一大波概念正在来袭: 作用域与命名空间 相关概念 与命名空间相关的概念有: 声明域(declaration region)—— 声明标识符的区域.如在函数外面声明的全局变量,它的声明域为 ...
- longestCommonPrefix
Description: Write a function to find the longest common prefix string amongst an array of strings. ...
- 微信小程序入门一
基本的准备工作 -知识储备 --基础:HTML+JS+CSS --进阶:React.Vue -工具安装 --工具由微信官方提供 ---下载地址:https://github.com/zce/weapp ...
- hashmap,hashTable concurrentHashMap 是否为线程安全,区别,如何实现的
线程安全类 在集合框架中,有些类是线程安全的,这些都是jdk1.1中的出现的.在jdk1.2之后,就出现许许多多非线程安全的类. 下面是这些线程安全的同步的类: vector:就比arraylist多 ...
- java I/O (一)
java 的I/O类基本上可以分为6大类:二进制的输入,二进制的输出,文本的输入,文本的输出,FILTER类和其他对文件操作的工具类. Java的I/O类主要在java.io的包里,其中两个主要的流为 ...
- 洛谷 P1462 解题报告
P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...
- 微信二维码支付-模式一(PC端,解决中文乱码问题)
近期公司调完银联,调支付宝,调完支付宝调微信.说实话微信的帮助文档确实是烂,而且有没有技术支持,害的我头发都掉了一桌.不说废话了,看代码. 首先登陆微信的公众平台(微信的服务号不是订阅号),然后选择微 ...
- React Native在特赞的应用与实践
基于React技术栈构建开发前端项目,并使用React Native开发特赞移动APP 目前正在使用Node.js开发和维护特赞服务网关,希望Node.js能够在更轻量级的微服务架构中发挥重要作用 课 ...
- javascript 原型机制
prototype,__proto__,constructor 在 JavaScript 原型继承结构里面,规范中用 [[Prototype]] 表示对象隐式的原型,在 JavaScript 中用 _ ...
- 在线OJ使用总结(acm)
赛码网OJ规则 用readLine()代替read_line() 用readLine()代替read_line() 用readLine()代替read_line() 用readLine()代替read ...