使用Long 类型的List 的contains 方法总是返回false

文章描述了一个在Java程序中,使用List查找数据库中Long类型正确答案时遇到的问题。尽管数据库中存储的是Long类型,但序列化后List中的元素实际上是Integer。因此,当尝试使用contains方法检查ID是否存在时,由于类型不匹配导致始终返回false。作者通过调试发现这个问题并提出了使用for循环和强制类型转换作为临时解决方案。
摘要由CSDN通过智能技术生成

问题来源: 数据库中有两张表 一张题目表 一张选项表 题目表中存放着选项表中正确选项的id(List存放) answerList

获取到所有选项表之后 想要知道哪几个选项是正确选项 要提取正确选项的名称 使用正确选项answerList的contains方法

每一个选项判断id 是否在 正确答案的选项中

// 查询出所有选项
List<TopicOptionsDO> options = topicOptionsMapper.selectList(TopicOptionsDO::getSubjectId, subjectDO.getId());
// 正确答案的list
List<Long> answerList = subjectDO.getSubAnswerList();

collect = options.stream().filter(option -> {
    // 选项id
    Long id = option.getId();
    // 判断是否存在
    boolean contains = answerList.contains(id);
    System.out.println(answerList);
    System.out.println("contains = " + contains+" "+"id="+id);
    return contains;
}).map(TopicOptionsDO::getOptionName).collect(Collectors.toList());

因为这个 collect 总是为 0 很是疑惑 便输出看下

[5, 6, 7]
contains = false id=5
[5, 6, 7]
contains = false id=6
[5, 6, 7]
contains = false id=7
[5, 6, 7]
contains = false id=8
[2]
contains = false id=1
[2]
contains = false id=2
[2]
contains = false id=3
[2]
contains = false id=4

[5,6,7] 是answer 判断5是否在这个list中 居然是false ?????

两个明明都是Long类型 查看了contains 的源码

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}
public int indexOf(Object o) {
    // 如果里面有null 的值 那么 查询list中是否为null 
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        // 不为null 时 循环遍历数组元素 与o的equals进行比较
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

查看一下Long类型的equals方法


/**
 * Compares this object to the specified object.  The result is
 * {@code true} if and only if the argument is not
 * {@code null} and is a {@code Long} object that
 * contains the same {@code long} value as this object.
 *
 * @param   obj   the object to compare with.
 * @return  {@code true} if the objects are the same;
 *          {@code false} otherwise.
 */
public boolean equals(Object obj) {
    if (obj instanceof Long) {
        return value == ((Long)obj).longValue();
    }
    return false;
}

可以看出 这个Long的equals 是将 比较对象转换成Long对象然后取longValue()值 如果相等 那么就是true

dubug一圈之后 发现 不走上述equals 的13行代码 于是我将 answerList中的元素拿出来和第一个 选项的值比较

answerList -> [5,6,7]

options.get(0) -> 5

options.get(0) -> 5

for (Long aLong : answerList) {
   TopicOptionsDO topicOptionsDO = options.get(0);
    System.out.println("topicOptionsDO.getId() = " + topicOptionsDO.getId());
    System.out.println("aLong = " + aLong);
    System.out.println("----------");
    System.out.println(aLong instanceof Long);
    System.out.println(topicOptionsDO.getId() instanceof Long);
    System.out.println("----------");
    System.out.println(aLong==topicOptionsDO.getId());
    System.out.println(aLong.equals(topicOptionsDO.getId()));
}

本想着看看结果 让我发现了意外

这下大家应该知道怎么个事了 .......

数据库类型 json java类型 List<Long>

序列化回来 泛型是Long 但是实际是Integer

使用fori 循环 遍历new 一个新的arrayList 暂时不知有什么其他的好方法 希望评论建议建议

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值