Java ArrayBlockingQueue remove()方法

Java ArrayBlockingQueue remove()方法

ArrayBlockingQueue 是有边界的、阻塞的队列,它在内部存储了由数组支持的元素。

  • ArrayBlockingQueue 类是Java集合框架的一个成员。
  • 有界意味着它将有一个固定的大小,你所存储的元素数量 不能 超过队列的容量。
  • 该队列也遵循FIFO(先进先出)规则来存储和移除队列中的元素。
  • 如果你试图把一个元素放进一个满的队列或从一个空的队列中取出一个元素,那么队列将阻止你。

remove(Object o) 方法从这个队列中 移除 指定元素的 一个实例 ,如果它存在的话。
我们可以说,如果这个队列包含一个或多个这样的元素,该方法就会移除一个元素e,即o.equals(e)。如果这个队列包含我们要删除的指定元素,Remove()方法返回真。

语法

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);
    }
}

输出 :

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]

参考资料:

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#remove(java.lang.Object)。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程