Introduction
A queue in Java is a data structure following the principle of FIFO (First-In-First-Out). The collection has a front and a rear end. The elements are added at the rear end of the queue collection and removed from the front. This interface is provided by the java.util.package extending to the Collection Interface. Queue supports many methods, such as inserting and deleting. In a java.util.package, the available queues are known as Unbounded Queues. Additionally, the queues in the java.util.concurrent packages are the Bounded Queues.
Read on to know what is a queue in Java with easy examples.
What Is a Queue?
A queue is an object that symbolizes a data structure that is intended to have elements added to the end and deleted from the beginning. Except for the Deques, all queues allow for addition at the tail and deletion from the front. Deques allow for the addition and removal of parts at both ends.
Application of the Java Queue
We must create a concrete class to use the queue interface. The few possible implementations are as follows:
- util.PriorityQueue
- util.LinkedList
PriorityBlockingQueue serves as a substitute for thread-safe implementation because these implementations are not thread-safe.
Our Learners Also Read: Array List in Java: All You Need to Know About it
Queue in Java Example
Example 1
// add elements in Java
// to a Queue
import java.util.*;
public class DAYS {
public static void main(String args[])
{
Queue
pq.add("Monday");
pq.add("Tuesday");
pq.add("Wednesday");
System.out.println(pq);
}
}
Output:
[Monday, Tuesday, Wednesday]
Example 2
// remove elements in Java Queue
public static void main(String[] arg) {
// declaring a variable
// Queue is an interface with two methods to add elements
Queue
queueOne.add(5); // add method to use insert element
queueOne.add(1);
queueOne.add(7);
queueOne.add(3);
queueOne.add(6);
System.out.println("Before remove and poll method The queue is: " + queueOne);
// poll method to remove top element
int positionOne = queueOne.poll();
System.out.println("Removed Element value from Queue : "+positionOne);
// remove method for eliminating the top element
int positionTwo = queueOne.remove();
System.out.println("Removed Element value from Queue : "+positionTwo);
System.out.println("After removal and poll method The queue is: " + queueOne);
}
Output:-
Before the remove and poll method, the queue is: [5, 1, 7, 3, 6]
Removed Element value from Queue: 5
Removed Element value from Queue: 1
After the removal and poll method, the queue is: [7, 3, 6]
Priority Queue Java
The collection framework's PriorityQueue class gives us the means for processing things as per their priority. The First-In-First-Out method governs queues, but occasionally it is necessary to process queue items by priority; this is when the PriorityQueue is useful.
Let's look at how to use the priority class in Java to generate a queue object.
FileName: ExampleCollection.java
import java.util.*;
class ExampleCollection{
public static void main(String args[]){
PriorityQueue
queue.add("Anu");
queue.add("Venu");
queue.add("Kirti");
queue.add("Jaya");
queue.add("Reena");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements:");
Iterator
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
Output:-
head: Anu
head: Anu
iterating the queue elements:
Anu
Jaya
Kirti
Venu
Reena
after removing two elements:
Kirti
Reena
Venu
Priority Queue Implementation Java
Before understanding priority queue implementation, let's see some of the main attributes of the PriorityQueue:
- It is an unbound queue.
- PriorityQueue does not permit null.
- It is not possible to establish a priority queue for non-comparable objects.
- The classes AbstractQueue, AbstractCollection, Collection, and Object are all descendants of PriorityQueue.
- The head or front of the queue contains the smallest element as per the natural ordering.
- The enforcement of the priority queue is not thread-safe. Therefore, one should use the PriorityBlockingQueue for synchronized access.
Let's understand the concept of priority in Java with the help of an example:
// Java program to show the
// PriorityQueue
import java.util.*;
class PriorityQueueDemo {
// Main Method
public static void main(String args[])
{
// Creating an empty priority queue
PriorityQueue
// Addition of items to the pQueue with add()
pQueue.add(5);
pQueue.add(10);
pQueue.add(15);
// Print the top element of PriorityQueue
System.out.println(pQueue.peek());
// Print the top element before removing it
// from the PriorityQueue container
System.out.println(pQueue.poll());
// Print the top element once again
System.out.println(pQueue.peek());
}
}
Output:-
5
5
15
This example explains below operations of the priority queue.
- boolean add(E element)
It inserts or places the specific element into a priority queue.
- public peek()
It restores but does not remove the head of the queue. It will return null if the queue is found empty.
- public poll()
It recalls and removes the head of the queue. It can return null if the queue is empty.
Thus for the priority queue, we can conclude:
- Null is not allowed in a PriorityQueue.
- A PriorityQueue containing non-comparable objects cannot be created.
- Unbound queues are PriorityQueue.
- According to the order provided, the item at the front of this queue has the least importance. The head is one of the elements in a tie for the element with the lowest value; ties are broken at random.
- Java offers a PriorityBlockingQueue class that implements the BlockingQueue interface to use in a Java Multithreading environment since PriorityQueue is not thread-safe.
- The operations used for queue retrieval include polling, removing, peeking, and element accessing the element at the front of the queue.
- For add and poll methods, it offers O(log(n)) time.
- It has methods that come from the classes of AbstractQueue, AbstractCollection, Collection, and Object.
Conclusion
Java is a widespread p