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 ...
随机推荐
- Codable实现json转Model,是时候干掉HandyJSON了!
自从开始使用Swift做项目,一直都在使用HandyJSON,不可否认,HandyJSON在Swift4.0是个好东西,也尝试过其它json转mode的工具,最终发现还是HandyJSON最好用. 去 ...
- Coursera-AndrewNg(吴恩达)机器学习笔记——第二周
一.多变量线性回归问题(linear regression with multiple variables) 搭建环境OctaveWindows的安装包可由此链接获取:https://ftp.gnu. ...
- 常用的几个在线生成网址二维码的API接口
原创,转载请注明出处! 用接口的好处就是简单,方便,时时更新,二维码生成以后不用保存在本项目服务器上面,可以减少不必要的开支,无需下载安装什么软件,可简单方便地引用,这才是最便捷的免费网址二维码生成 ...
- 关于cannot find module 'xxxx’的一个可能解决方法。
关于cannot find module 'xxxx'的一个可能解决方法. 由于学习angular2,想单独学习一下typescript下angular2使用的'rxjs'是怎么使用的,我用npm自己 ...
- Download and Install Apache Zookeeper on Ubuntu
http://www.techburps.com/misc/download-and-install-apache-zookeepr/36 In previous article of this Bi ...
- Python_字符串格式化
#冒泡排序 array = [1,2,3,6,5,4] for i in range(len(array)): for j in range(i): if array[j] > array[j ...
- 关于 JavaScript 中的复制数组
之前在写扫雷的时候,因为需要用到二维数组,当时就在复制数组这里出现了问题,所以记录一下. 当我们在需要复制数组的时候一定需要注意,数组是复合的数据类型,直接复制的话,只是复制了指向底层数据结构的指针, ...
- PAT1028:List Sorting
1028. List Sorting (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Excel ca ...
- asp.net mvc 使用 Autocomplete 实现类似百度,谷歌动态搜索条提示框。
Autocomplete是一个Jquery的控件,用法比较简单. 大家先看下效果: 当文本框中输入内容,自动检索数据库给出下拉框进行提示功能. 需要用此控件大家先到它的官方网站进行下载最新版本: ht ...
- 解决持久化数据太大,单个节点的硬盘无法存储的问题;解决运算量太大,单个节点的内存、CPU无法处理的问题
需要学习的技术很多,要自学新知识也不是一件容易的事,选择一个自己比较感兴趣的会是一个比较好的开端,于是,打算学一学分布式系统. 带着问题,有目的的学习,先了解整体架构,在深入感兴趣的细节,这是我的计划 ...