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的更多相关文章

  1. Cannot be cast to java.lang.Comparable异常

    Set集合中的treeSet问题:cannot be cast to java.lang.Comparable: 原理: Set不保存重复的元素,与Collection类似,只是行为不同,Set是基于 ...

  2. Set集合中的treeSet问题:cannot be cast to java.lang.Comparable;

    使用TreeSet保存自定义对象时, 必须让定义对象的类实现Comparable接口,并重写compareTo()方法 否则报 实体类User:cannot be cast to java.lang. ...

  3. TreeMap cannot be cast to java.lang.Comparable

    /** * Constructs a new, empty tree map, using the natural ordering of its * keys. All keys inserted ...

  4. java.lang.Long cannot be cast to java.lang.Integer解决办法

    情景: mybatis连接oracle 报错: 测试增的时候,报错 Java.lang.Long cannot be cast to  java.lang.Integer:删改没有报错. 排查过程: ...

  5. [记录]java.math.biginteger cannot be cast to java.lang.long

    可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...

  6. java.lang.Comparable接口

    转自:http://blog.csdn.net/zccst/article/details/5092920 java.lang.Comparable 接口 作者: zccst java.lang.Co ...

  7. 利用泛型抽取Dao层,加事务注解问题(java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType)

    想利用泛型抽取BaseDao层,简化操作时出现故障: @Transactional这个注解是能够继承的.于是就想写在抽取的BaseDao层上,让实现的类能够不用写@Transactional,就可开启 ...

  8. Mybatis的失误填坑-java.lang.Integer cannot be cast to java.lang.String

    Mybatis的CRUD小Demo 为方便查看每次的增删改结果,封装了查询,用来显示数据库的记录: public static void showInfo(){ SqlSession session ...

  9. Java.lang.Comparable接口和Java.util.Comparator接口的区别

    Java的Comparator和Comparable当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序. 1.Com ...

随机推荐

  1. redis资源未释放引发的问题

    一.redis资源未释放的起因: N年前,在修改一个古老程序时,不小心把redis释放的这块给干掉了, if (jedis != null) { if (!isInProcess) { jedis.d ...

  2. Java并发-线程安全性

    首先了解一下多线程的概念 多线程:两段或以上的代码同时进行,多个顺序执行流. 并发和并行的区别 并发:做一下这个做一下那个. 并行:同时进行. 线程和进程的区别 进程:资源分配的基本单位,运行中的程序 ...

  3. 【原创】整合Spring4+Hibernate4+Struts2时NullPointerException问题解决

    1.开场白 相信SSH初学者肯定遇到过这个问题,但是又是百思不得其解,明白了之后就恍然大悟. 2.问题描述 程序实现过程是UserAction中调用UserService,UserService的实现 ...

  4. C语言pow()函数的计算精度问题

    编程计算 a+aa+aaa+-+aa-a(n个a)的值,n和a的值由键盘输入.例如,当n=4,a=2,表示计算2+22+222+2222的值. 程序运行结果示例: Input a,n: 2,4↙ su ...

  5. Math对象中比较常用的计算数学相关的三个方法

    Math类中提供了三个与取整有关的方法:ceil.floor.round,这些方法的作用与它们的英文名称的含义相对应,例如: ceil的英文意义是天花板,该方法就表示向上取整,所以,Math.ceil ...

  6. Unity3D学习(七):Unity多重采样抗锯齿设置无效的解决办法

    前言 学习Shader的过程中发现模型锯齿严重,于是去Edit--Project Settings--Quality选项下将反锯齿设置为了8X Multi Sampling.结果没有任何改变,如图: ...

  7. 深入出不来nodejs源码-编译启动(1)

    整整弄了两天,踩了无数的坑,各种奇怪的error,最后终于编译成功了. 网上的教程基本上都过时了,或者是版本不对,都会报一些奇怪的错误,这里总结一下目前可行的流程. node版本:v10.1.0. 首 ...

  8. PiggyMetrics windows 部署

    PiggyMetrics 是springcloud的demo,其特性就不细说了,主要描述在win10下部署的坑. 官网是:https://github.com/sqshq/PiggyMetrics 官 ...

  9. Spring Boot中使用Actuator的/info端点输出Git版本信息

    对于Spring Boot的Actuator模块相信大家已经不陌生了,尤其对于其中的/health./metrics等强大端点已经不陌生(如您还不了解Actuator模块,建议先阅读<Sprin ...

  10. ucloud发送短信的php sdk

    在ucloud官方的版本中,只有python的sdk可供调用,现提供php的sdk发送短信 项目地址:https://github.com/newjueqi/ucloudsms 使用方法: (1)在c ...