【Java报错】java.lang.ClassCastException: xxxClass cannot be cast to java.lang.Comparable 问题重现+解决

简介: 【Java报错】java.lang.ClassCastException: xxxClass cannot be cast to java.lang.Comparable 问题重现+解决

报错信息:java.lang.ClassCastException: xxx cannot be cast to java.lang.Comparable

1. 问题重现

以下为伪代码:

// 获取的List对象集合
  List<SomeRes> someResList = service.getSomeResList();
  // 要放入的Set集合
  Set<SomeRes> someResSet = null;
  for (SomeRes res : someResList ) {
    someResSet = new TreeSet<>();
    someResSet.add(res);
  }

原因分析:

第一次添加元素时,因为TreeSet或者TreeMap对象为空,不需要比较,不会报错。但是当第二次放入元素时,TreeSet或者TreeMap为了确保对象有序的就必须比较,这个时候发现这两个对象根本无法比较,则抛出该异常错误。实际上在new TreeSet<>(); 时idea已经提示(Construction of sorted collection with non-comparable elements 构造具有非可比元素的排序集合)

2. 问题解决

非可排序的类实现 Comparable 接口,并重写 compareTo 方法:

public class SomeRes implements Comparable<SomeRes> {
    @ApiModelProperty(value = "设备ID")
    @JsonProperty(value = "equip")
    private String equip;
    @ApiModelProperty(value = "车牌号")
    @JsonProperty(value = "car_number")
    private String carNumber;
    @Override
    public int compareTo(@NotNull SomeRes o) {
        return this.carNumber.compareTo(o.carNumber);
    }
}
目录
相关文章
|
30天前
启动报错:java.nio.charset.MalformedInputException: Input length = 1
启动报错:java.nio.charset.MalformedInputException: Input length = 1
16 0
|
1月前
|
Java 编译器
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
有关电脑中idea编译报错问题java: No implementation was created for AdminUserConverter due to having a problem in
34 0
|
17天前
|
Java Maven
【Java报错】显示错误“Error:java: 程序包org.springframework.boot不存在“
【Java报错】显示错误“Error:java: 程序包org.springframework.boot不存在“
35 3
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
|
3月前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
56 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
3月前
|
Linux Windows
FinalShell连接Linux虚拟机报错java.net.ConnectException: Connection timed out: connect(亲测有效)
FinalShell连接Linux虚拟机报错java.net.ConnectException: Connection timed out: connect(亲测有效)
174 0
|
1天前
|
JavaScript Serverless API
Serverless 应用引擎操作报错合集之在Serverless 应用引擎中,FC3.0读取response body的时候出现错误提示"Caused by: java.io.IOException: closed"如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
11 3
|
16天前
|
Java 应用服务中间件 Maven
使用IDEA搭建SpringMVC环境,Maven导入了依赖,但是运行报错 java.lang.ClassNotFoundException
使用IDEA搭建SpringMVC环境,Maven导入了依赖,但是运行报错 java.lang.ClassNotFoundException
14 1
|
20天前
|
存储 Java 数据库连接
java使用mp持久化框架,写入5000个字符,但是VARCHAR(255) 会报错
使用Java的MyBatis Plus框架时,如果尝试将超过VARCHAR(255)限制的字符串(如5000个字符)存入数据库,会抛出异常。解决方法是将列类型改为TEXT。可通过在实体类属性上添加`@TableField(typeHandler = JdbcType.CLOB)`注解,如`private String content;`,将属性映射到CLOB类型列,以存储更长字符串。
9 0
|
1月前
|
Java 关系型数据库 MySQL
Flink1.18.1和CDC2.4.1 本地没问题 提交任务到服务器 报错java.lang.NoClassDefFoundError: Could not initialize class io.debezium.connector.mysql.MySqlConnectorConfig
【2月更文挑战第33天】Flink1.18.1和CDC2.4.1 本地没问题 提交任务到服务器 报错java.lang.NoClassDefFoundError: Could not initialize class io.debezium.connector.mysql.MySqlConnectorConfig
54 2