Queue Class

This lesson provides some more detail on the built-in methods of the queue class in Java.

We'll cover the following

Queue

The queue interface is available in the java.util package and extends the Collection interface. It operates in a first in first out (FIFO) order. Elements are inserted at the back (end) and are deleted from the front end.

Being an interface, the queue needs a concrete class for the declaration and the most common classes in Java are the PriorityQueue and LinkedList.

The methods supported by queue are :

add() - Add elements at the tail of queue.

peek() - View the head of the queue without removing it. Returns Null if the queue is empty.

element() - Returns the element at the front of the queue. Throws an exception when the queue is empty.

remove() - Removes and returns the head of the queue. Returns null if the queue is empty. Throws an exception when queue is empty.

poll() - Removes and returns the head of the queue.

size() - Returns the number of elements in the queue.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.