Java ArrayBlockingQueue peek() Method7 Jan 2025 | 2 min read The peek() method of Java ArrayBlockingQueue class retrieves the head of this queue, but doesn't remove it. The method returns null if this queue is empty. Syntax:Parameters:NA Specified By:The peek() method of ArrayBlockingQueue class is specified by peek() method in interface Queue Return Value:The peek() method returns this queue's head and if this queue is empty,it returns null. Example 1Output: Head of queue : Reema Queue : [Reema, Rahul, Rita, Ramesh] Example 2Output: Hinna got 97 marks. Vinni got 87 marks. Rima got 77 marks. Topper of the class is Hinna Example 3Output: Head of queue is : null Next TopicJava-arrayblockingqueue-poll-method |
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
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 clear() method of ArrayBlockingQueue class automatically withdraws all the elements from this priority queue. After calling this method, the queue will be empty. Syntax: public void clear() Parameters: NA Specified By: The clear() method of class ArrayBlockingQueue is specified by clear() method in interface Collection. Override: The clear() method of class ArrayBlockingQueue overrides...
2 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 iterator() method of ArrayBlockingQueue class returns an iterator across the elements of this queue in a sequential manner. The elements returned will follow order from first (head) to last (tail). Syntax: public Iterator<E> iterator() Parameters: NA Specified By: The iterator() method of ArrayBlockingQueue class is specified by: iterator() method in interface Collection<E>. iterator()...
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
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 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 take() method of Java ArrayBlockingQueue class retrieves and removes the first element of the ArrayBlockingQueue, waiting (if necessary) until an element becomes available. Syntax: public E take() throws InterrupedException Parameters: NA Specified By: The take() method of ArrayBlockingQueue class is specified by: take in interface BlockingQueue<E>. Return Value: The take() method returns the...
3 min read
The forEach() method of Java ArrayBlockingQueue class executes the specified action for each element of Iterable till all the elements are processed or the action throws an exception. Syntax: public void forEach(Consumer<? super E> action) Parameters: action - This is the action to be performed for every element. Specified By: The...
4 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