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 ...
随机推荐
- Ocelot中文文档-授权
Ocelot支持基于声明的授权. 这意味着如果您有要授权的路由,您可以将以下内容添加到您的ReRoute配置中. "RouteClaimsRequirement": { " ...
- es6(四):Symbol,Set,Map
1.Symbol: Symbol中文意思"象征" Symbol:这是一种新的原始类型的值,表示独一无二的值(可以保证不与其它属性名冲突) Symbol()函数前面不能使用new,因 ...
- windows系统下输入法图标显示设置
原先任务栏有两个搜狗输入法的标志,还有一个"中/英"的图标:甚至桌面还悬浮这一个搜狗输入法图标. 打开vscode等工具时,桌面悬浮的图标有时可能会遮挡到一些信息,十分不爽. 如今 ...
- Hbase出现ERROR: Can't get master address from ZooKeeper; znode data == null解决办法
问题描述如下: hbase(main)::> list TABLE ERROR: Can't get master address from ZooKeeper; znode data == n ...
- 常用的几个在线生成网址二维码的API接口
原创,转载请注明出处! 用接口的好处就是简单,方便,时时更新,二维码生成以后不用保存在本项目服务器上面,可以减少不必要的开支,无需下载安装什么软件,可简单方便地引用,这才是最便捷的免费网址二维码生成 ...
- 报错:严重: Servlet.service() for servlet [springmvc] in context with path [ ] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
解决:service类或dao类需要@Autowired
- mint-ui之picker爬坑记
picker的数据来源为动态获取时,数据无法正常渲染!因为方法不对,所以坑大了!深刻地体会到'业不精,我之过',谨以此文,深刻地记录一下踩坑及爬坑的整个过程,以便日后不再入坑,也给后来者提供一下参考 ...
- .net自定义错误页面实现
前言: 在实际的web开发中,经常会遇到以下情况,导致给用不好的体验: a.程序未处理的异常,直接输出显示到用户页面 b.用户访问的资源不存在,直接显示系统默认的404页面 c.其它以下请求错误状态的 ...
- cropper(裁剪图片)插件使用(案例)
公司发布微信H5前端阵子刚刚弄好的H5端的图片上传插件,现在有需要裁剪图片.前端找了一个插件---cropper 本人对这插件不怎么熟悉,这个案例最好用在一个页面只有一个上传图片的功能上而且只适合单个 ...
- Cookie与 Session使用详解
Cookie概念 在浏览某些 网站 时,这些网站会把 一些数据存在 客户端 , 用于使用网站 等跟踪用户,实现用户自定义 功能. 是否设置过期时间: 如果不设置 过期时间,则表示这个 Cookie生命 ...