Queue ADT

Queue Interface

  • follows the FIFO (First In First Out) principle

  • an ordered list of objects with its use limited to inserting elements at the end of the list and deleting elements from the start of the list

  • Declaration: 

    public interface Queue extends Collection

CREATING QUEUE OBJECTS

  • always need a class which extends this list in order to create an object

  • Obj is the type of object to be stored in Queue

    Queue<Obj> queue = new PriorityQueue<Obj>();

OPERATIONS IN QUEUE INTERFACE

  • Adding Elements

    • add() method: insertion order is not retained in the PriorityQueue; elements are stored based on the priority order (ascending by default)

  • Removing Elements

    • remove() method: if multiple. First occurrence will be removed

    • poll() method: used to remove the head and return it

  • Iterating the Queue

    • convert the queue to the array and traversing using the loop; queue also has an inbuilt iterator

CHARACTERISTICS

  • used to insert elements at the end of the queue and removes from the beginning of the queue (FIFO concept)

  • supports all methods of Collection interface including the insertion, deletion, etc.

  • LinkedList, ArrayBlockingQueue and PriorityQueue are the most frequently used implementations

  • if any null operation is performed on BlockingQueues, NullPointerException is thrown

  • the Queues which are available in java.util package are Unbounded Queues

  • all Queues except the Deques supports insertion and removal at the tail and head of the queue respectively. The Deques support element insertion and removal at both ends.

CLASSES THAT IMPLEMENT THE QUEUE INTERFACE

PriorityQueue

  • provides a way to process the objects based on the priority

LinkedList

  • inherently implements the linked list data structure

PriorityBlockingQueue

  • alternative implementation if thread-safe implementation is needed

  • unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations

  • since it is unbounded, adding elements may sometimes fail due to resource exhaustion resulting in OutOfMemoryError