zoukankan      html  css  js  c++  java
  • 解决List遍历删除元素提示ConcurrentModificationException

    JDK1.8提供新的API  ===>  removeIf

      

    public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("A");
            list.add("B");
            list.add("C");
            list.add("D");
         list.removeIf(e
    -> "C".equals(e));
         
    list.forEach(System.out::println);
    }

    运行后结果

    上边的写法还是繁琐,可以再进行简写:

    public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("A");
            list.add("B");
            list.add("C");
            list.add("D");
            list.removeIf("C"::equals);
            list.forEach(System.out::println);
    }

    JDK1.8真香!

                                                          -----路漫漫其修远兮,吾将上下而求索.愿你我共勉

  • 相关阅读:
    Flask商城项目详解
    《剑指offer》题解(Python版本)
    小程序学习笔记
    Html5 语义化标签
    常用页面布局
    css 语法记录
    vue-router 配置
    axios 配置
    Mybatis Generator
    openssl_pkcs7_verify的问题之旅
  • 原文地址:https://www.cnblogs.com/llysc/p/13273079.html
Copyright © 2011-2022 走看看