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);
          
    
      }
相关推荐
wangchunyu1142 小时前
JAVA CompletableFuture异步编程实战
java·开发语言·python
2501_944521594 小时前
Immer 跨端三方库仓颉适配全流程
python·eclipse·django
信誓旦旦的程序猿4 小时前
【Python量化实战宝典 #26】止损与回撤控制手册:移动止损、最大回撤(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
2501_944521594 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github
Patrick在香港4 小时前
同一行 strftime,两台机器跑出两种日期
python·datetime·时区·locale·日期处理
2601_967987374 小时前
用Python+OpenCV+YOLO写个游戏脚本:从自动瞄准到自动采集的保姆级教程
python·opencv·yolo·目标检测·游戏自动化
meiying88385 小时前
快递单号批量查询Python脚本:从手动到自动化的完整实现
开发语言·python·自动化
人工智能导论实践课5 小时前
【GUI桌面开发3】PyCharm配置外部工具
ide·python·pycharm
一千柯橘5 小时前
LangChain 的实时输出处理: stream_events 和 Streaming
python·langchain·agent
LannyChung5 小时前
PowerShell 下 git log 中文乱码的完整排查与解决
windows·git