Java ArrayBlockingQueue Class

ArrayBlockingQueue is a bounded blocking queue which orders the element in FIFO(first-in-first-out). In this queue, new elements are inserted at the tail of this queue and the elements are retrieved from the head of this queue.

ArrayBlockingQueue class implements all the optional methods of the Collection and Iterator interfaces and is a member of the Java Collections Framework.

ArrayBlockingQueue Methods

MethodDescription
add()This method returns a Boolean value true if it inserts the specified element at the tail of this queue.
clear()This method removes all the elements automatically from this queue.
contains()This method returns a Boolean value true if this queue contains the specified element.
drainTo()This method removes all the available elements or the given atmost number of elements from this queue and adds them to the collection.
forEach()This method performs the action for each element until all elements have been processed.
iterator()This method returns an iterator over the elements in proper sequence.
offer()This method inserts the specified element at the tail of this queue, waiting up to the specified wait time for space to become available if the queue is full.
peek()This method retrieves the head of the queue without removing it.
poll()This method retrieves and removes the head of this queue.
put()This method inserts the specified element at the tail of this queue.
remainingCapacity()This method returns the capacity of the elements which this queue can accept without blocking.
remove()This method removes the specified element from the queue if it is present in the queue.
removeAll()This method removes all the elements of this in the queue which are present in the specified collection.
removeIf()This method removes all the elements in this queue that satisfy the given predicate filter.
retainAll()This method retains only those elements in this queue that are present in the specified collection.
size()This method returns the number of the elements in this queue.
spliterator()This method returns a spliterator over the elements in this queue.
toArray()This method returns an array containing all the elements of this queue which are in proper sequence.
toString()This method returns a string representation of this collection.
take()This method retrieves and removes the first element of the ArrayBlockingQueue.

Example 1

Output:

Elements :
Reema
Rahul
Rita
Ramesh
Gita
Geetanjali
queue.contains(reema) will return true

Example 2

Output:

Size of the queue : 10
Queue = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Remaining capacity : 40
 Even number = [1, 3, 5, 7, 9]
Remaining capacity : 45
Head of the element : 1
Head of the element : 3
Queue : [3, 5, 7, 9]