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


Java ArrayBlockingQueue removeAll()用法及代碼示例


Java ArrayBlockingQueue 類的 removeAll() 方法刪除指定集合中包含的 ArrayBlockingQueue 元素。

用法:

public boolean removeAll(Collection<?> c)

參數:

這裏,參數 c 是包含要從 ArrayBlockingQueue 中刪除的元素的集合。

指定者:

ArrayBlockingQueue 類的 removeAll() 方法指定為:

接口 Collection<E> 中的 removeAll。

返回值:

如果集合因調用而改變,removeAll() 方法返回一個布爾值 'true',否則返回 'false'。

拋出:

如果指定的集合不支持空元素或包含一個或多個空元素,則拋出 NullPointerException。

例子1

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueueRemoveAllExample1 {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> list = new ArrayList<Integer>();
        int capacity = 100;
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
       for (int i=1;i<11;i++){
        queue.add(i);}
        System.out.println("Total no:"+ queue);
        for (int i=1;i<11;i++){
            i=i+1;
            list.add(i);}
     //removes all the elements int the queue which are present in list
       queue.removeAll(list);
        System.out.println(" Even no:"+list);
        System.out.println(" Odd no:"+queue);
    }
}

輸出:

Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Even no:[2, 4, 6, 8, 10]
Odd no:[1, 3, 5, 7, 9]

例子2

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

class Patients{
    String Pname;
    public Patients(String pname) {
        Pname = pname;
    }
    public String toString(){
        return this.Pname;
    }
}
public class ArrayBlockingQueueRemoveAllExample2 {
    public static void main(String[] args) {
        Patients patient1 = new Patients("Megha");
        Patients patient2 = new Patients("Reema");
        Patients patient3 = new Patients("Supriya");
        Patients patient4 = new Patients("Anoop");
        Patients patient5 = new Patients("Sonia");
        Patients patient6 = new Patients("Tejashvi");
        List<Patients> list = new ArrayList<Patients>();
        int capacity = 100;
        ArrayBlockingQueue<Patients> queue = new ArrayBlockingQueue<Patients>(capacity);
        queue.add(patient1);
        queue.add(patient2);
        queue.add(patient3);
        queue.add(patient4);
        queue.add(patient5);
        queue.add(patient6);
        list.add(patient1);
        list.add(patient2);
        list.add(patient3);
        list.add(patient4);
        System.out.println("Total Patients = "+queue.size());
        for(Patients xyz:queue){
            System.out.println(xyz);
        }
        System.out.println();
        System.out.println("Patients recovered and discharged:"+list);
        System.out.println();
        //removes all the elements int the queue which are present in list
        queue.removeAll(list);
        System.out.println("Patients left in the hospital = "+queue.size());
        for (Patients xyz:queue) {
            System.out.println(xyz);
        }
    }
}

輸出:

Total Patients = 6
Megha
Reema
Supriya
Anoop
Sonia
Tejashvi

Patients recovered and discharged:[Megha, Reema, Supriya, Anoop]

Patients left in the hospital = 2
Sonia
Tejashvi

例子3

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueueRemoveAllExample3 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        int capacity = 100;
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
        queue.add(123);
        queue.add(56);
        queue.add(100);
        System.out.println("Queue = "+queue);
        queue.removeAll(null);
        System.out.println("List = "+list);
    }
}

輸出:

Queue = [123, 56, 100]
Exception in thread "main" java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.util.AbstractCollection.removeAll(AbstractCollection.java:371)
	at com.javaTpoint.ArrayBlockingQueueRemoveAllExample3.main(ArrayBlockingQueueRemoveAllExample3.java:16)




相關用法


注:本文由純淨天空篩選整理自 Java ArrayBlockingQueue removeAll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。