
int size(): Gives the number of elements in the queue, or queue size.void clear(): Removes all elements from the queue.E peek(): Gets a reference to the highest priority element in the queue, but does not remove it.E poll(): Removes the highest priority element, posited it in the front of the queue.


This class provides the facility to insert data according to the underlying data sorted manner. We also can supply a Comparator object through the PriorityQueue constructor during its creation. It orders elements according to the natural ordering of the elements, as specified by Comparable’s compareTo() method. The PriorityQueue class is one of the concrete implementations of the Queue interface and is also one of the most commonly used. Therefore, it has all the capabilities of a collection and additionally provides operations such as inserting, removing, and inspecting elements in a queue. The Queue interface in the Java API library is an extension of the Collection interface. There, it can be said that a queue typically behaves in a FIFO manner, but not necessarily so. Apart from this, there are many variations to it, such as priority queues, where elements are ordered according to the natural ordering of the elements or according to a supplied comparator. If we create a queue, slightly altering its behavior to LIFO (Last-in, First-out), it becomes a stack. This ordering principle is very similar to the LinkedList class that implements the List interface. This FIFO idea illustrates the predictive ability of the queue in general. As a result, additional nodes are queued up until it can be routed. The routing node routes one packet at a time. Each time a packet arrives at a node, it must be routed to the next node along the path until final destination is reached. In a network router, information packets wait in a queue.As a result, although more than one print job is submitted, the printer picks up the most recent one from the queue. Queues are also used in print spooling, where print jobs are submitted in a queue.The application at the front of the queue get the first chance to be serviced.

Each application requiring processor time is placed in a queue. It, therefore, can service only one application at a time.

Imagine a computer having a single processor.Now, you may have been pondering what their uses may be, especially in computer system. The person standing first takes the ticket and gets out of the queue first, whereas the person standing last in the line is the last to be out. This ordering principle can very easily be understood analogically by people standing in a queue to get, say, a bus ticket. This means that the order of store and retrieval of elements in a queue is such that, elements which are stored first are the ones to be retrieved first. OverviewĪ queue, in its basic sense, is a linear collection of elements that follows a specific store and retrieval behavior called FIFO, or First-in and First-out. This article delineates the working concept along with a tinge of API classes working behind the scenes in the Java Collection API library. The Queue interface, along with its many variations, is one of them. These classes are built in a manner to hold generic elements this means it can hold elements of any but the same consecutive type designated at the time of declaration of the collection object. The behavior is defined by the characteristic of the data structure it implements. The collection library of Java provides classes and interfaces to hold elements according to a specified norm prior to processing.
