当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayBlockingQueue removeIf()用法及代码示例


Java ArrayBlockingQueue 类的 removeIf() 方法删除满足给定谓词过滤器的 ArrayBlockingQueue 元素。

用法:

public boolean removeIf(Predicate<? Super E> filter)

参数:

参数过滤器是一个谓词,它为要删除的元素返回真。

指定者:

ArrayBlockingQueue 类的 removeIf() 方法指定为:

removeIf in interface Collection<E>.

返回值:

removeIf() 方法返回一个布尔值 'true' 如果集合删除了任何元素,否则返回 'false'

抛出:

如果指定的过滤器为空,则抛出 NullPointerException。

例子1

import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.Predicate;

public class ArrayBlockingQueueRemoveIfExample1 {
    public static void main(String[] args) throws InterruptedException {
       int capacity = 100;
       ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
       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.ArrayBlockingQueue;
import java.util.function.Predicate;

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;
    }
}
public class ArrayBlockingQueueRemoveIfExample2 {
    public static void main(String[] args) {
        int capacity = 100;
        ArrayBlockingQueue<IsVoter> queue = new ArrayBlockingQueue<IsVoter>(capacity);
        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);
        }
    }
}

输出:

People eligible to vote:
Name = Reema  Age = 18
Name = Kajol  Age = 37
Name = Ravi  Age = 47

例子3

import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.Predicate;

public class ArrayBlockingQueueRemoveIfExample3 {
    public static void main(String[] args) {
        int capacity = 100;
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
        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);
    }
}

输出:

Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Exception in thread "main" java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.util.Collection.removeIf(Collection.java:410)
	at com.javaTpoint.ArrayBlockingQueueRemoveIfExample3.main(ArrayBlockingQueueRemoveIfExample3.java:15)

如果指定的 Predicate 过滤器为 null,它将为您提供 NullPointerException,如上例所述。





相关用法


注:本文由纯净天空筛选整理自 Java ArrayBlockingQueue removeIf() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。