Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
queue
interface

What is the Java Queue interface?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Queue interface is available in java.util package and extends the collection interface. A queue1 is an ordered list of objects with its use limited to insert elements at the end of a list and deleting elements from the start of a list. It follows the First-In-First-Out (FIFO) principle.

Below is a visualization of the insertion of objects in a queue. Objects are always inserted at the tail of the queue.

1 of 5

Below is a visualization of the removal of objects in a queue. Objects are always removed from the head of the queue.

1 of 5

The most common classes that implement Queue interface in Java are:

  • PriorityQueue
  • LinkedList
svg viewer

Commonly used methods

  • add(): is used to add an element at the tail of the queue. The element to be inserted is passed as the parameter. The method throws an exception if the operation is unsuccessful, and returns true if the operation is successful.

  • offer(): is used to add an element at the tail of the queue. The element to be inserted is passed as the parameter. The method returns false if the operation is unsuccessful, and returns true if the operation is successful.

  • remove(): returns the head of the queue and removes it. The method throws an exception if the queue is empty.

  • poll(): the remove() method returns the head of the queue and removes it. It returns null if the queue is empty.

  • peek(): returns the head of the queue without removing it. It returns null if the queue is empty.

  • element(): the peek() method returns the head of the queue without removing it. It throws an exception if the queue is empty.

  • size(): returns the number of elements in the queue.

  • isEmpty(): returns true if the queue is empty, otherwise it returns false.

Other implementing classes

  • AbstractQueue
  • ArrayBlockingQueue
  • ArrayDeque
  • ConcurrentLinkedDeque
  • ConcurrentLinkedQueue
  • DelayQueue
  • LinkedBlockingDeque,
  • LinkedBlockingQueue
  • LinkedTransferQueue,
  • PriorityBlockingQueue,
  • PriorityQueue
  • SynchronousQueue

RELATED TAGS

java
queue
interface
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring