Java ArrayBlockingQueue add() Method7 Jan 2025 | 3 min read The add() method of ArrayBlockingQueue() appends the defined element at the tail end of the queue if the queue's capacity allows it. The method returns true on successful insertion and throws an IllegalStateException if queue doesn't has enough capacity. Syntax:Parameters:e - This is the element to be added. Specified By:The add() method of class ArrayBlockingQueue is specified by:
Override:The add() method of class ArrayBlockingQueue overrides the add() method of class AbstractQueue<E>. Throws:The add() method throws : NullPointerException - If the element defined is null. IllegalStateException - If the queue is full. Return Value:The add() method returns true(as defined by Collection.add(E)). Example 1Output: After adding 1 to the queue : [1] After adding 2 to the queue : [1, 2] After adding 3 to the queue : [1, 2, 3] After adding 4 to the queue : [1, 2, 3, 4] After adding 5 to the queue : [1, 2, 3, 4, 5] Example 2Output: 1 Lower case = aman Upper case = AMAN 2 Lower case = bhavika Upper case = BHAVIKA Example 3Output: 71018 8000 1178 1190 Max number = 71018 Min number = 1178 Example 4Output: Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ArrayBlockingQueue.checkNotNull(ArrayBlockingQueue.java:150) at java.util.concurrent.ArrayBlockingQueue.offer(ArrayBlockingQueue.java:325) at java.util.AbstractQueue.add(AbstractQueue.java:95) at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312) at com.javaTpoint.ArrayBlockingQueue_addMethodExample4.main(ArrayBlockingQueue_addMethodExample4.java:16) If any specified element in the queue is null, it will give you NullPointerException as shown above. Next TopicJava-arrayblockingqueue-clear-method |
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...
3 min read
The toString() method of Java ArrayBlockingQueue returns a string representation of this collection. Syntax: public String toString() Parameters: NA Overrides toString in class AbstractionCollection<E> Return Value: The toString() method returns a string representation of this collection. Example 1 import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class ArrayBlockingQueueToStringExample1 { public static void main(String[] args) { ...
2 min read
The Spliterator() method of Java ArrayBlockingQueue class returns a Spliterator over the elements in this ArrayBlockingQueue. Syntax: public Spliterator<E> spliterator() Parameters: NA Specified By: The Spliterator () method of ArrayBlockingQueue class is specified by: Spliterator in interface Collection<E>. Spliterator in interface Iterable<E> Return Value: The Spliterator () method returns a Spliterator over the elements in...
4 min read
The contains() method of Java ArrayBlockingQueue() class returns true if the defined element is present in this queue. Syntax: public boolean contains(Object o) Parameters: o - This is the object that is to be checked for occurrence in this queue. Specified By: The contains() method of ArrayBlockingQueue class is specified by: contains()...
2 min read
The retainAll() method of Java ArrayBlockingQueue class retains only the elements of the ArrayBlockingQueue that are contained in the specified collection and removes rest of the elements. Syntax: public boolean retainAll(Collection<?> c) Parameters: Here, the parameter c is the collection containing elements to be retained in the ArrayBlockingQueue collection. Specified...
3 min read
The removeIf() method of Java ArrayBlockingQueue class removes the elements of the ArrayBlockingQueue that satisfies the given predicate filter. Syntax: public boolean removeIf(Predicate<? Super E> filter) Parameters: The parameter filter is a predicate which returns true for the elements to be removed. Specified By: The removeIf() method of ArrayBlockingQueue class is...
3 min read
The offer() method of Java ArrayBlockingQueue class adds the defined element at the tail of this queue, only if the queue is not full or it waits till the specified wait time for space to get free, if the queue is full. Syntax: 1.public boolean offer(E e) 2.public...
4 min read
The toArray() method of Java ArrayBlockingQueue returns an array containing all the elements of the ArrayBlockingQueue, in proper sequence. The syntax of toArray() returns an array where the runtime of the returned array is that of the specified array, and if the queue fits in...
6 min read
The poll() method of ArrayBlockingQueue class retrieves the head of this queue awaiting till the specified time if essential for an element to be available, and also removes it. The method returns null if this queue is empty. Syntax: 1.public E poll() 2.public E poll(long timeout,TimeUnit unit)throws InterruptedException Parameters: NA timeout...
3 min read
The drainTo() method of ArrayBlockingQueue() class removes all the elements present in this queue and adds them to the provided collection. Syntax: public int drainTo(Collection<? super E> c) public int drainTo(Collection<? super E> c, int maxElements) Parameters: c - This is the collection to which elements are transferred. maxElements - This...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India