當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ConcurrentLinkedQueue removeIf()用法及代碼示例


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 removeIf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。