ArrayBlockingQueue
is a bounded queue and it is thread-safe. Internally, it uses a fixed-size array. Once the object is created, the size cannot be changed. The elements of this queue follow. Also, First-In-First-Out Elements are inserted at the end of the queue and retrieved from the head of the queue. null
objects are not allowed as elements.
The contains
method can be used to check if an element is present in the queue.
public boolean contains(Object o)
This method takes the element to be checked for availability as an argument.
This method returns true
if the element is present in the queue. Otherwise, false
will be returned.
The code below demonstrates how to use the contains
method.
import java.util.concurrent.ArrayBlockingQueue;class Contains {public static void main( String args[] ) {ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(5);queue.add("1");queue.add("2");queue.add("3");System.out.println("The queue is " + queue);System.out.println("queue.contains(1) - " + queue.contains("1"));System.out.println("queue.contains(4) - " + queue.contains("4"));}}
In the above code:
We imported the ArrayBlockingQueue
from the java.util.concurrent
package.
We created an object for the ArrayBlockingQueue
class with the name queue
and size 5.
We added three elements to the queue
object.
We used the contains
method to check if an element is present in the queue.
"1"
, the contains
method returns true
."4"
, the contains
method returns false
.