Last Updated on December 14, 2022 by Prepbytes
Firstly, we’ll see what the queue is.
Queue
A queue is basically a linear data structure that works on the principle of FIFO (First in First out) which means an element that is enqueued first will be dequeued first. Element is enqueued from the rear end of the queue and dequeued from the front end. The insertion operation in the queue is known as enqueue and the deletion operation in the queue is known as dequeue.
The queue is used when the operations are to be performed in the manner of First in First out order just like Breadth-First Search.
For Example A ticket Queue outside a cinema hall where the person enters the queue first will get the ticket first.
Now coming to the queue interface, The queue interface is present in java.util package and extends the collection interface is used to hold the elements about to be processed in FIFO(first In First Out) manner.
Interface of the queue needs a specific class for the declaration and in java most common classes are the priority queue and linked list.
The queue interface is declared as follow:
public interface queue extends collection
Interface:
In java, interface is defined as an abstract class which is used to specify the behavior of the class. An interface is a blueprint of class.
A java interface contains static constant and abstract methods.
We can say that interfaces can have variables and abstract methods.
Syntax:
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
For declaring the interface, we’ll use the interface keyword. Interfaces are used to provide total abstraction i.e. in interface all the methods are declared with an empty body and are public and also the fields are public, static, and final by default.
Implementation:
import java.io.*; interface In1 { final int a = 10; void display(); } class TestClass implements In1 { public void display(){ System.out.println("PrepBytes"); } public static void main(String[] args) { TestClass t = new TestClass(); t.display(); System.out.println(a); } }
Creating queue objects: Since queue is an interface, objects can not be created of type queue. We need a class which extends the list in order to create an object.
Queue<obj> queue = new PriorityQueue<obj>();
Implementation:
import java.util.LinkedList; import java.util.Queue; class QueueExample { public static void main(String[] args) { Queue<Integer> q = new LinkedList<>(); for (int i = 0; i < 8; i++) q.add(i); System.out.println("Elements of queue " + q); int removedele = q.remove(); System.out.println("removed element-" + removedele); System.out.println(q); int head = q.peek(); System.out.println("head of queue-" + head); int size = q.size(); System.out.println("Size of queue-" + size); } }
Operations of Priority Queue:
- Adding element: To insert an element into the priority queue we’ll use add() method. In the priority queue, all the elements are stored based on their priority i.e. in ascending order by default.
Code:
import java.util.*; import java.io.*; public class PriorityQueueDemo { public static void main(String args[]) { PriorityQueue<Integer> pq = new PriorityQueue<>(); for(int i=0;i<3;i++){ pq.add(i); pq.add(1); } System.out.println(pq); } }
- Removing elements: To remove an element from the priority queue, we’ll use remove() method. If there are multiple objects which will be removed then the element which occurs first will be removed first. ALso we can use the poll() method to remove and return it.
Code:
import java.util.*; import java.io.*; public class PriorityQueueDemo { public static void main(String args[]) { PriorityQueuepq = new PriorityQueue<>(); pq.add("hello"); pq.add("Prep"); pq.add("Bytes"); System.out.println("Initial PriorityQueue " + pq); pq.remove("prep"); System.out.println("After Remove - " + pq); System.out.println("Poll Method - " + pq.poll()); System.out.println("Final PriorityQueue - " + pq); } }
- Accessing the elements: As we know, Queue follows FIFO(First In First Out)principle, therefore we can access only the head of the queue. To access the element we’ll use the peek() method.
Code:
import java.util.*; class PriorityQueueDemo { public static void main(String[] args) { PriorityQueue<String> pq = new PriorityQueue<>(); pq.add("Hello"); pq.add("Prep"); pq.add("bytes"); System.out.println("PriorityQueue: " + pq); String element = pq.peek(); System.out.println("Accessed Element: " + element); } }
- Iterating the priority queue: There are multiple ways to traverse the priority queue, but the most efficient and famous way is to convert the queue into an array and then traverse it using a for loop. Also, queues have an inbuilt iterator for iterating the queue.
Code:
import java.util.*; class PriorityQueueDemo { public static void main(String args[]) { PriorityQueue<String> pq = new PriorityQueue<>(); pq.add("Hello"); pq.add("Prep"); pq.add("Bytes"); Iterator iterator = pq.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }
Methods of queue interface:
- add(index, element): This method is used to add an element at a particular index in the queue.
- addAll(index,collection): This method is used to add all the elements in the given collection of the queue.
- size(): This method is used to return the size of the element.
- clear(): This method is used to remove all the elements from the queue.
- remove(element): This method is used to remove and return the first occurrence of the given element from the queue.
- remove(index): This method is used to remove the element from the given index.
This article tried to discuss the concept of Queue Interface using Java. Hope this blog helps you understand the concept. To practice problems you can check out MYCODE | Competitive Programming at Prepbytes