List直接使用removeAll报错

List直接使用removeAll报错

需要先将list转换才能使用

原因是:

removeAll 方法在 Java 中用于从当前列表中删除另一个列表中存在的所有元素。如果直接对 List 接口的一个实现使用 removeAll 方法抛出异常,可能的原因有:

  1. 不同的List实现 :如果你尝试在不支持该操作的 List 实现上使用 removeAll,例如 Arrays.asList() 创建的列表,则会抛出 UnsupportedOperationException

  2. 并发修改异常 :如果在迭代过程中尝试修改列表,会抛出 ConcurrentModificationException

    解决方法:

  • 确保你使用的 List 实现支持 removeAll 操作,如 ArrayList, LinkedList, Vector 等。

    复制代码
      public static void main(String[] args) {
    
          List<String> list1 = Arrays.asList("CS562798662", "CS563279854", "CS563276666");
          List<String> list2 = Arrays.asList("CS562798662", "CS563279854");
          ArrayList<Object> list11 = new ArrayList<>();
          ArrayList<Object> list22 = new ArrayList<>();
          list11.addAll(list1);
          list22.addAll(list2);
    
          list11.removeAll(list22);
          System.out.println(list11);
          
    
      }
相关推荐
2601_966949655 分钟前
批量 K 线不只是提速:因子研究中的数据质量、复权与回测偏差
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
一位正在转型AI全栈的前端工程师6 分钟前
AI 全栈学习之旅 -Week 11:从 CLI 到浏览器:用 FastAPI、SSE 和 Vue 3 做一个可审核的 ReAct Agent
前端·python
洛阳纸贵12 分钟前
AI-PyTorch(一)基础代码实操和自动求导
人工智能·pytorch·python
点亮~黑夜14 分钟前
远程连接浏览无桌面服务器上的图片
windows·软件构建
2601_9622845020 分钟前
uv:终结 Python 虚拟环境管理乱局
python·项目管理·uv·虚拟环境·依赖管理
Albart57521 分钟前
Docker Desktop 增量更新失败终极排查:Failed to apply delta update 反复回滚怎么办?
windows·docker·容器·docker部署大模型
Patrick在香港29 分钟前
Claude + Python 数据管道:一次字段改名,786 行历史数据被静默重写
开发语言·windows·python·claude·数据工程·数据校验·数据管道
the局外人1 小时前
学习 FastAPI 的 Day 3:企业级目录与数据库迁移
后端·python·fastapi
Ray13331 小时前
交付级定时数据管道:改 JSON 声明调度,84 个单测全绿
python
mldong1 小时前
AI Agent 不能自己签字:用 Python 工作流引擎给 AI 加一道人类审批闸门
后端·python·agent