ArrayBlockingQueue remove() method in Java
Last Updated : 26 Nov, 2018
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.
- ArrayBlockingQueue class is a member of the Java Collections Framework.
- Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
- The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
- If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.
The
remove(Object o) method
removes a single instance of the specified element from this queue, if it is present. We can say that method removes an element e such that o.equals(e) if this queue contains one or more such elements. Remove() method returns true if this queue contained the specified element which we want to remove.
Syntax: public boolean remove(Object o)
Parameter: o - element to be removed from this queue, if present.
Returns: returns true if this queue contained the specified element which we want to remove. Below program illustrate remove(Object o) method of ArrayBlockingQueue.
Example 1 Java // 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]
Example 2 Java // 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]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#remove(java.lang.Object) Similar Reads
ArrayBlockingQueue Class in Java In Java, the ArrayBlockingQueue class is part of the java.util.concurrent package and implements the BlockingQueue interface. It is a thread-safe, bounded queue that helps manage producer-consumer scenarios by blocking threads when the queue is full or empty.The queue has a fixed size, specified dur
8 min read
ArrayBlockingQueue add() method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
3 min read
ArrayBlockingQueue clear() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue al
2 min read
ArrayBlockingQueue contains() method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
3 min read
ArrayBlockingQueue drainTo() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue al
5 min read
ArrayBlockingQueue iterator() Method in Java The iterator() method of ArrayBlockingQueue class is used to returns an iterator of the same elements as this queue in a proper sequence. The elements returned from this method contains elements in order from first(head) to last(tail). The returned iterator is weakly consistent. Syntax: public Itera
2 min read
ArrayBlockingQueue offer() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
6 min read
ArrayBlockingQueue peek() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue al
2 min read
ArrayBlockingQueue poll() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
4 min read
ArrayBlockingQueue put() method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
2 min read