ConcurrentLinkedQueue 类的 removeIf() 方法删除该队列中满足给定谓词过滤器的元素。
用法:
public boolean removeIf(Predicate<? Super E> filter)
参数:
参数过滤器是一个谓词,它为要删除的元素返回真。
指定者:
ConcurrentLinkedQueue 类的 removeIf() 方法指定为:
接口 Collection<E> 中的 removeIf。
返回值:
如果集合删除了任何元素,removeIf() 方法返回一个布尔值 'true',否则返回 'false'。
抛出:
如果指定的过滤器为空,则抛出 NullPointerException。
例子1
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
public class ConcurrentLinkedQueueRemoveIfExample1 {
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
for (int i=1;i<21;i++){
queue.add(i);}
System.out.println("Total no:"+ queue);
//removes all the elements which satisfies the predicate filter
Predicate<Integer> pr= a->(a>10);
queue.removeIf(pr );
System.out.println(" Number less than 11 = "+queue);
}
}
输出:
Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Number less than 11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
例子2
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
public class ConcurrentLinkedQueueRemoveIfExample2 {
public static void main(String[] args) {
ConcurrentLinkedQueue<IsVoter> queue = new ConcurrentLinkedQueue<IsVoter>();
IsVoter isVoter1 = new IsVoter("Reema",18);
IsVoter isVoter2 = new IsVoter("Raj",7);
IsVoter isVoter3 = new IsVoter("Kajol",37);
IsVoter isVoter4 = new IsVoter("Ravi",47);
IsVoter isVoter5 = new IsVoter("Varun",17);
queue.add(isVoter1);
queue.add(isVoter2);
queue.add(isVoter3);
queue.add(isVoter4);
queue.add(isVoter5);
//removes all the elements which satisfies the Predicate filter
Predicate<IsVoter> pr= (IsVoter age) ->(age.age < 18);
queue.removeIf(pr );
System.out.println(" People eligible to vote:");
for (IsVoter xyz:queue ){
System.out.println(xyz);
}
}
}
class IsVoter {
String name;
int age;
public IsVoter(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name = " + this.name + " Age = " + this.age;
}
}
输出:
People eligible to vote: Name = Reema Age = 18 Name = Kajol Age = 37 Name = Ravi Age = 47
例子3
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
public class ConcurrentLinkedQueueRemoveIfExample3 {
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
for (int i=1;i<21;i++){
queue.add(i);}
System.out.println("Total no:"+ queue);
//removes all the elements which satisfies the predicate filter
Predicate<Integer> pr=null;
queue.removeIf(pr );
System.out.println(queue);
}
}
输出:
Exception in thread "main" java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] at java.util.Collection.removeIf(Collection.java:410) at com.javaTpoint.ConcurrentLinkedQueueRemoveIfExample3.main(ConcurrentLinkedQueueRemoveIfExample3.java:13)
相关用法
- Java ConcurrentLinkedQueue removeAll()用法及代码示例
- Java ConcurrentLinkedQueue remove()用法及代码示例
- Java ConcurrentLinkedQueue retainAll()用法及代码示例
- Java ConcurrentLinkedQueue add()用法及代码示例
- Java ConcurrentLinkedQueue offer()用法及代码示例
- Java ConcurrentLinkedQueue spliterator()用法及代码示例
- Java ConcurrentLinkedQueue size()用法及代码示例
- Java ConcurrentLinkedQueue forEach()用法及代码示例
- Java ConcurrentLinkedQueue iterator()用法及代码示例
- Java ConcurrentLinkedQueue contains()用法及代码示例
- Java ConcurrentLinkedQueue peek()用法及代码示例
- Java ConcurrentLinkedQueue isEmpty()用法及代码示例
- Java ConcurrentLinkedQueue addAll()用法及代码示例
- Java ConcurrentLinkedQueue poll()用法及代码示例
- Java ConcurrentLinkedQueue toArray()用法及代码示例
- Java ConcurrentLinkedDeque add()用法及代码示例
- Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代码示例
- Java ConcurrentLinkedDeque removeLast()用法及代码示例
- Java ConcurrentLinkedDeque hashCode()用法及代码示例
- Java ConcurrentLinkedDeque contains()用法及代码示例
注:本文由纯净天空筛选整理自 Java ConcurrentLinkedQueue removeIf() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。