Java Queue
Java Queue
Queue Interface
The Queue interface is provided in java.util package and it implements the Collection
interface. The queue implements FIFO i.e. First In First Out. This means that the
elements entered first are the ones that are deleted first. A queue is generally used to
hold elements before processing them. Once an element is processed then it is removed
from the queue and next item is picked for processing.
boolean add(E e)
This method inserts the specified element into this queue if it is possible to
1 do so immediately without violating capacity restrictions, returning true upon
success and throwing an IllegalStateException if no space is currently
available.
E element()
2
This method retrieves, but does not remove, the head of this queue.
Page 2 of 6
boolean offer(E e)
3 This method inserts the specified element into this queue if it is possible to
do so immediately without violating capacity restrictions.
E peek()
4 This method retrieves, but does not remove, the head of this queue, or
returns null if this queue is empty.
E poll()
5 This method retrieves and removes the head of this queue, or returns null if
this queue is empty.
E remove()
6
This method retrieves and removes the head of this queue.
Methods Inherited
This interface inherits methods from the following interfaces −
java.util.Collection
java.lang.Iterable
LinkedList
ArrayDeque
PriorityQueue
The following are the interfaces that extend the Queue interface -
Deque
BlockingQueue
BlockingDeque
Open Compiler
package com.tutorialspoint;
import java.util.LinkedList;
import java.util.Queue;