CopyonWriteArraySet方法的removeIf ()方法会从此CopyonWriteArraySet中删除满足指定条件的元素。
使用方法:
publicbooleanremoveif (预制过滤器) )。
参数:此方法接受基于从集合中删除的元素的谓词值的必需参数过滤器。
返回值:如果CopyOnWriteArraySet更改,此方法将返回布尔值,如true。 否则,此方法返回false。
异常:如果指定的谓词过滤器为null,则此方法抛出NullPointerException。
以下示例程序用于说明CopyOnWriteArrayList类的removeIf ()函数。
样本1:
//javaprogramtoillustratethecopyonwritearrayset
//removeif(methodinJava
import Java.util.concurrent.copyonwritearrayset;
import java.util.*;
公共类gfg {
publicstaticvoidmain (字符串[ ] args ) )。
{
//createobjectofcopyonwritearrayset
版权所有
=new CopyOnWriteArraySet (;
//添加元素
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;
//打印副本写入器阵列
system.out.println (版权所有者ritearrayset : )。
ArrSet;
//if a number in the set is
//divisible by 3,then remove it
arrset.remove if (编号-编号%3==0);
//printupdatedcopyonwritearrayset
系统. out.println (updatedcopyonwritearrayset : )。
ArrSet;
}
}
输出:
版权所有: [ 10、20、30、40、50、60、70、80、90 ]
updatedcopyonwritearrayset : [ 10、20、40、50、70、80 ]
样本2:
//javaprogramtoillustratethecopyonwritearrayset
//removeif(methodinJava
import Java.util.concurrent.copyonwritearrayset;
import java.util.*;
公共类gfg {
publicstaticvoidmain (字符串[ ] args ) )。
{
//createobjectofcopyonwritearrayset
版权所有
=new CopyOnWriteArraySet (;
//添加元素
arrset.add(geeksforgeeks );
arrset.add(gfg );
arrset.add(geeks );
arrset.add(gfg );
//打印副本写入器阵列
system.out.println (版权所有者ritearrayset : )。
ArrSet;
try {
//if a number in the set is
//divisible by 3,then remove it
//thiswillthrownullpointerexception
arrset.remove if (空;
}
catch(exceptione ) {
系统. out.println (e;
}
}
}
输出:
版权所有: [ geeksforgeeks,GFG,geeks,Gfg]
java.lang.NullPointerException