CopyonWriteArraySet方法的removeIf()方法从此CopyOnWriteArraySet中移除满足指定条件的元素。
用法:
public boolean removeIf (Predicate<E> filter)
参数:此方法接受强制性参数过滤器,该过滤器是基于从该集合中删除哪些元素的谓词值。
返回值:如果CopyOnWriteArraySet更改,则此方法返回布尔值,例如true。否则,此方法返回false。
异常:如果指定的谓词过滤器为null,则此方法将引发NullPointerException。
以下示例程序旨在说明CopyOnWriteArrayList类的removeIf()函数:
示例1:
// Java Program to illustrate the CopyOnWriteArraySet
// removeIf() method in Java
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet<Integer> ArrSet
= new CopyOnWriteArraySet<Integer>();
// Add elements
ArrSet.add(10);
ArrSet.add(20);
ArrSet.add(30);
ArrSet.add(40);
ArrSet.add(50);
ArrSet.add(60);
ArrSet.add(70);
ArrSet.add(80);
ArrSet.add(90);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet: "
+ ArrSet);
// if a number in the set is
// divisible by 3, then remove it
ArrSet.removeIf(number -> number % 3 == 0);
// print updated CopyOnWriteArraySet
System.out.println("Updated CopyOnWriteArraySet: "
+ ArrSet);
}
}
输出:
CopyOnWriteArraySet: [10, 20, 30, 40, 50, 60, 70, 80, 90] Updated CopyOnWriteArraySet: [10, 20, 40, 50, 70, 80]
示例2:
// Java Program to illustrate the CopyOnWriteArraySet
// removeIf() method in Java
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet<String> ArrSet
= new CopyOnWriteArraySet<String>();
// Add elements
ArrSet.add("GeeksforGeeks");
ArrSet.add("GFG");
ArrSet.add("Geeks");
ArrSet.add("Gfg");
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet: "
+ ArrSet);
try {
// if a number in the set is
// divisible by 3, then remove it
// This will throw NullPointerException
ArrSet.removeIf(null);
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
CopyOnWriteArraySet: [GeeksforGeeks, GFG, Geeks, Gfg] java.lang.NullPointerException
相关用法
- Java CopyOnWriteArrayList removeIf()用法及代码示例
- Java LinkedBlockingDeque removeIf()用法及代码示例
- Java LinkedTransferQueue removeIf()用法及代码示例
- Java CopyOnWriteArraySet forEach()用法及代码示例
- Java CopyOnWriteArraySet removeAll()用法及代码示例
- Java CopyOnWriteArraySet addAll()用法及代码示例
- Java ArrayList removeIf()用法及代码示例
- Java Vector removeIf()用法及代码示例
- Java CopyOnWriteArraySet contains()用法及代码示例
- Java CopyOnWriteArraySet add()用法及代码示例
- Java CopyOnWriteArraySet clear()用法及代码示例
- Java CopyOnWriteArraySet retainAll()用法及代码示例
- Java CopyOnWriteArraySet equals()用法及代码示例
- Java CopyOnWriteArraySet spliterator()用法及代码示例
注:本文由纯净天空筛选整理自ProgrammerAnvesh大神的英文原创作品 CopyOnWriteArraySet removeIf() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。