Java Queue Interface

Category: Java   Tags: Java, Java Collection, Java Queue Interface

The Queue interface extends Collection and declares the behaviour of a queue. A queue is nothing but a first-in, first-out list. Queue is a generic interface:

                          interface Queue<E>
                                

Here, E specifies the type of objects that the queue will contain.

Methods

boolean add(E element)
Insert element to this queue and returns true upon success. Throws IllegalStateException if no space is currently available.
boolean offer(E element)
It attempts to add element to this queue. Returns true if element was added and false otherwise.
E remove()
Removes and returns the element at the head of the queue. Unlike poll() it throws NoSuchElementException exception if this queue is empty.
E poll()
Removes and returns the element at the head of the queue. Returns null if this queue is empty.
E element()
Retrieves, but does not remove, the head of this queue. Unlike peek() it throws NoSuchElementException exception if this queue is empty.
E peek()
Retrieves, but does not remove, the head of this queue. Returns null if this queue is empty.