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);
          
    
      }
相关推荐
TomEval5 小时前
【测AI】第02篇:Python 环境搭建与基础语法速通
人工智能·python·功能测试·aigc
归去来 兮5 小时前
LangChain从入门到精通
python·langchain·langgraph
YCOSA20255 小时前
BUG修复---雨晨 Windows 11 IoT 企业版 LTSC 23H2 终结版 22631.7588
windows·物联网
ayaya_mana5 小时前
Qwen3.5-9B融合DeepSeek-V4-Flash小模型实测与部署
windows·本地大模型·qwen3.5
淘气的小猴子6 小时前
Python 魔术方法入门
python
我要见SA姐16 小时前
DeepSeek Harness 开源贡献手记:从 Issue 到 Merge 的完整旅程
ide·vscode·python·自动化·编辑器
沉下心来学鲁班6 小时前
初识DeepAgents搭建第一个智能体
人工智能·python·langchain
ctlover7 小时前
数据结构:树
数据结构·python
触底反弹8 小时前
🐍 Python 语法全景图:一个 print() 引发的深度探索
python
元Y亨H8 小时前
告别依赖地狱与打包崩溃:Python 项目环境治理与 PyInstaller 避坑实践
python