ArrayBlockingQueue是有界的阻塞隊列,該隊列內部使用數組支持的元素存儲。
- ArrayBlockingQueue類是Java Collections Framework的成員。
- 有界意味著它將具有固定的大小,您不能存儲數量超過隊列容量的元素。
- 隊列還遵循FIFO(先進先出)規則,用於存儲和刪除隊列中的元素。
- 如果您嘗試將一個元素放入一個完整的隊列或從一個空隊列中取出一個元素,那麽該隊列將阻塞。
remove(Object o)方法從此隊列中刪除指定元素的單個實例(如果存在)。
我們可以說,如果此隊列包含一個或多個這樣的元素,則該方法將刪除滿足條件o.equals(e)的元素。如果此隊列包含要刪除的指定元素,則Remove()方法返回true。
用法:
public boolean remove(Object o)
參數:
o –要從此隊列中刪除的元素(如果存在)。
返回值:
如果此隊列包含要刪除的指定元素,則返回true。
以下示例程序旨在說明ArrayBlockingQueue的remove(Object o)方法。
例子1
// Java Program Demonstrate remove(Object o)
// method of ArrayBlockingQueue.
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// define capacity of ArrayBlockingQueue
int capacity = 5;
// create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> queue
= new ArrayBlockingQueue<Integer>(capacity);
// Add elements to ArrayBlockingQueue using put method
queue.put(223);
queue.put(546);
queue.put(986);
// print Queue
System.out.println("queue contains " + queue);
// remove 223
boolean response = queue.remove(223);
// print Queue
System.out.println("Removal of 223 :" + response);
// print Queue
System.out.println("queue contains " + queue);
// remove 546
response = queue.remove(546);
// print Queue
System.out.println("Removal of 546 :" + response);
// print Queue
System.out.println("queue contains " + queue);
// remove 734 which is not present
response = queue.remove(734);
// print Queue
System.out.println("Removal of 734 :" + response);
// print Queue
System.out.println("queue contains " + queue);
}
}
Output : queue contains [223, 546, 986] Removal of 223 :true queue contains [546, 986] Removal of 546 :true queue contains [986] Removal of 734 :false queue contains [986]
例子2
// Java Program Demonstrate remove(Object o)
// method of ArrayBlockingQueue.
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// define capacity of ArrayBlockingQueue
int capacity = 5;
// create object of ArrayBlockingQueue
ArrayBlockingQueue<String> queue
= new ArrayBlockingQueue<String>(capacity);
// Add elements to ArrayBlockingQueue using put method
queue.put("StarWars");
queue.put("SuperMan");
queue.put("Flash");
queue.put("BatMan");
queue.put("Avengers");
// print Queue
System.out.println("queue contains " + queue);
// remove StarWars
boolean response = queue.remove("StarWars");
// print Queue
System.out.println("Removal of StarWars :" + response);
// print Queue
System.out.println("queue contains " + queue);
// remove Hulk
response = queue.remove("Hulk");
// print Queue
System.out.println("Removal of Hulk :" + response);
// print Queue
System.out.println("queue contains " + queue);
}
}
Output : queue contains [StarWars, SuperMan, Flash, BatMan, Avengers] Removal of StarWars :true queue contains [SuperMan, Flash, BatMan, Avengers] Removal of Hulk :false queue contains [SuperMan, Flash, BatMan, Avengers]
相關用法
- Java ArrayBlockingQueue contains()用法及代碼示例
- Java ArrayBlockingQueue put()用法及代碼示例
- Java ArrayBlockingQueue add()用法及代碼示例
- Java ArrayBlockingQueue take()用法及代碼示例
- Java ArrayBlockingQueue size()用法及代碼示例
- Java ArrayBlockingQueue drainTo()用法及代碼示例
- Java ArrayBlockingQueue toArray()用法及代碼示例
- Java ArrayBlockingQueue toString()用法及代碼示例
- Java ArrayBlockingQueue spliterator()用法及代碼示例
- Java ArrayBlockingQueue iterator()用法及代碼示例
- Java ArrayBlockingQueue poll()用法及代碼示例
- Java ArrayBlockingQueue peek()用法及代碼示例
- Java ArrayBlockingQueue clear()用法及代碼示例
- Java ArrayBlockingQueue offer()用法及代碼示例
- Java ArrayBlockingQueue remainingCapacity()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayBlockingQueue remove() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。