当前位置:网站首页>Collection delete element technique removeif

Collection delete element technique removeif

2022-06-10 23:56:00 Li_ XiaoJin

removeIf

removeIf() It's from JDK1.8 Start offering .

see API file :

Delete all elements of this collection that satisfy the given predicate . Errors or runtime exceptions thrown in iterations or predicates are forwarded to the caller .

We deleted List Elements in , Generally, loop traversal is used to realize . Found today removeIf useful , Make a note of .

Previous practice :

        List<String> testList = new ArrayList<>();
        Iterator<String> it = testList.iterator();
        while (it.hasNext()) {
            if ("aa".equals(it.next())) {
                it.remove();
            }
        }

Now you can use :

        testList.removeIf(s -> "aa".equals(s));
    //  perhaps 
        testList.removeIf("aa"::equals);

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/list Delete element technique removeif

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102238037568.html