Queue Interface

Welcome to the blog about Queue Interface. In this article, I am going to introduce a concept of Queues and methods divided into two groups, depending on their behavior, with the focus on PriorityQueues and PriorityBlockingQueues. I hope you will enjoy the content and gain some new experience.

Interface Queue<E> extends Collection<E> Class and represents a collection intended to hold the elements about to be processed. Besides the operations that inherit from Collection, Queue provides operations such as insertion, extraction, and inspection. Each operation has two directions:

Queues usually order elements in a first-in-first-out (FIFO) data structure algorithm. In a FIFO queue, all new elements are added at the tail of the queue. There are also a last-in-first-out (LIFO) queues or stacks, but it’s important to note that whatever the ordering is used, the element on the head of the queue will be removed by calling methods remove() or poll(). About LIFO data structures, stacks and deques will be more words in the article Deques.

The Queue is just an Interface and if we want to declare Queues, the PriorityQueue and LinkedList Classes are usually used. Both implementations are not thread-safe and as their alternative, if a thread-safe implementation is needed, PriorityBlockingQueue is recommended.

Methods inherited from Interface java.util.Collection: addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray.

Example:

         Queue<String> fruits = new LinkedList<>(); 

         // Adding fruits  
         fruits.add("Banana");
         fruits.add("Apple");
         fruits.add("Orange");
         fruits.add("Avocado");

         // Display contents of the queue
         System.out.println("Fruits in queue: " + fruits); 

         // Removing the head of the queue
         String removeElement = fruits.remove(); 
         System.out.println("Removed element: " + removeElement); 

         System.out.println("Fruits in queue: " + fruits); 

         // Viewing the head of the queue 
         String head = fruits.peek(); 
         System.out.println("Head of queue: " + head); 

PriorityQueue Class

Class PriorityQueue<E> extends AbstractQueue Class and implements Serializable Interface. It represents an unbounded priority queue based on a priority heap. The elements are ordered according to their natural ordering or by a Comparator (depending on which constructor is used). A priority queue is unbounded but has an internal capacity which grows automatically when elements are added.

PriorityQueue does not permit Null elements and insertion of non-comparable objects. This implementation is not synchronized which means that multiple threads should not access a PriorityQueue, alternatively, PriorityBlockingQueue should be used.

Some usual methods are listed below, for further information feel free to check the official website:

Constructors:

Example:

         // Creating a PriorityQueue
         PriorityQueue<String> fruits = new PriorityQueue<>();

         // Adding fruits
         fruits.add("Banana");
         fruits.add("Orange");
         fruits.add("Apple");
         fruits.add("Avocado");

         System.out.println("Fruits in queue: " + fruits);

         // Remove a fruit from the PriorityQueue
         fruits.remove();

         System.out.println("Fruits in queue: " + fruits);  

PriorityBlockingQueue Class

PriorityBlockingQueue<E> extends AbstractQueue<E> Class and implements BlockingQueue<E> and Serializable Interfaces. It represents an unbounded blocking queue and uses the same ordering as PriorityQueue. Instead of FIFO order, this queue orders its elements based on their natural ordering. Just like the PriorityQueue, PriorityBlockingQueue doesn’t permit Null elements and insertion of non-comparable objects.

The ordering of elements with equal priority is not guaranteed but you can define custom classes or comparators that use a secondary key to break ties in primary priority values.

Some usual methods are listed below, for further information feel free to check the official website:

Constructors:

Example:

         // Creating list of numbers
         LinkedList<Integer> list = new LinkedList<Integer>();
         list.add(5);
         list.add(25);
         list.add(2);
         list.add(15);    	
    	
         BlockingQueue<Integer> queue = new PriorityBlockingQueue<>(list);

         // Printing numbers in unspecified order
         System.out.println(queue);

If you liked this article, you might be interested in Lists, Sets, Maps, Queues and Deques. Feel free to browse.