CopyOnWriteArrayList的removeIf()方法從此CopyOnWriteArrayList中移除滿足指定條件的元素。
用法:
public boolean removeIf (Predicate<E> filter)
參數:此方法接受強製性參數過濾器,該過濾器是基於從該列表中刪除哪些元素的謂詞值。
返回值:如果CopyOnWriteArrayList更改,則此方法返回布爾值,例如true。否則,此方法返回false。
異常:如果指定的謂詞過濾器為null,則此方法將引發NullPointerException。
以下示例程序旨在說明CopyOnWriteArrayList類的removeIf()函數:
// Java Program to illustrate the CopyOnWriteArrayList
// removeIf() method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArrayList
CopyOnWriteArrayList<Integer> ArrLis
= new CopyOnWriteArrayList<Integer>();
// Add elements
ArrLis.add(2);
ArrLis.add(3);
ArrLis.add(4);
ArrLis.add(7);
ArrLis.add(6);
ArrLis.add(9);
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: "
+ ArrLis);
// if a number in the List is
// divisible by 3, then remove it
ArrLis.removeIf(number -> number % 3 == 0);
// print updated CopyOnWriteArrayList
System.out.println("Updated CopyOnWriteArrayList: "
+ ArrLis);
}
}
輸出:
CopyOnWriteArrayList: [2, 3, 4, 7, 6, 9] Updated CopyOnWriteArrayList: [2, 4, 7]
// Java Program to illustrate the CopyOnWriteArrayList
// removeIf() method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArrayList
CopyOnWriteArrayList<String> ArrLis
= new CopyOnWriteArrayList<String>();
// Add elements
ArrLis.add("GeeksforGeeks");
ArrLis.add("GFG");
ArrLis.add("Geeks");
ArrLis.add("Gfg");
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: "
+ ArrLis);
try {
// This will throw NullPointerException
ArrLis.removeIf(null);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
CopyOnWriteArrayList: [GeeksforGeeks, GFG, Geeks, Gfg] java.lang.NullPointerException
相關用法
- Java LinkedTransferQueue removeIf()用法及代碼示例
- Java LinkedBlockingDeque removeIf()用法及代碼示例
- Java CopyOnWriteArraySet removeIf()用法及代碼示例
- Java CopyOnWriteArrayList set()用法及代碼示例
- Java CopyOnWriteArrayList addAllAbsent()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java CopyOnWriteArrayList subList()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
- Java CopyOnWriteArrayList forEach()用法及代碼示例
- Java CopyOnWriteArrayList remove()用法及代碼示例
- Java CopyOnWriteArrayList equals()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
- Java Vector removeIf()用法及代碼示例
- Java ArrayList removeIf()用法及代碼示例
注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 CopyOnWriteArrayList removeIf() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。