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);
          
    
      }
相关推荐
风逸hhh1 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮1 小时前
Python训练第四十三天
开发语言·python
互联网杂货铺2 小时前
完美搭建appium自动化环境
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
Gyoku Mint3 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
莱茵菜苗3 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长3 小时前
Python 构建法律DeepSeek RAG
开发语言·python
luojiaao4 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
英英_4 小时前
视频爬虫的Python库
开发语言·python·音视频
猛犸MAMMOTH4 小时前
Python打卡第46天
开发语言·python·机器学习
多多*4 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap