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);
          
    
      }
相关推荐
用户8356290780515 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng86 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi7 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee7 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay7 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤8 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean9 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽9 小时前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户606487671889610 小时前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听11 小时前
RAG深入学习之Chunk
前端·人工智能·python