Methods of Queue

The Queue interface includes all the methods of the Collection interface. It is because Collection is the super interface of Queue. Some of the commonly used methods of the Queue interface are:

  1. add() - Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.
  2. offer() - Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.
  3. element() - Returns the head of the queue. Throws an exception if the queue is empty.
  4. peek() - Returns the head of the queue. Returns null if the queue is empty.
  5. remove() - Returns and removes the head of the queue. Throws an exception if the queue is empty.
  6. poll() - Returns and removes the head of the queue. Returns null if the queue is empty.