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);
          
    
      }
相关推荐
空影星1 分钟前
ValiDrive:一键验证USB真实容量
python·智能手机·django·flask
喵了几个咪11 分钟前
游戏字体渲染
开发语言·python·游戏
Blossom.11819 分钟前
RLHF的“炼狱“突围:从PPO到DPO的工业级对齐实战
大数据·人工智能·分布式·python·算法·机器学习·边缘计算
vvoennvv30 分钟前
【Python TensorFlow】CNN-BiLSTM时序预测 卷积神经网络-双向长短期记忆神经网络组合模型(附代码)
python·神经网络·cnn·tensorflow·lstm·bilstm
数据知道44 分钟前
【Flask】一文掌握 Flask 基础用法
数据库·后端·python·flask·python web
Mr数据杨1 小时前
【Gradio】Gradio 启动规避 Huggingface 代理问题
python·gradio
s***87271 小时前
【Python】网络爬虫——词云wordcloud详细教程,爬取豆瓣最新评论并生成各式词云
爬虫·python·信息可视化
薛定e的猫咪2 小时前
【调试技巧】vscode 四种断点调试,快速定位 bug
ide·vscode·python·bug
CadeCode2 小时前
Python 开发环境与包管理
python
爱写代码的小朋友2 小时前
Python局域网远程监控电脑屏幕实现
python·flask·python监控电脑屏幕