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.
We must create a concrete class to use the queue interface. The few possible implementations are as follows:
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
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]
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
Before understanding priority queue implementation, let's see some of the main attributes of the PriorityQueue:
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.
It inserts or places the specific element into a priority queue.
It restores but does not remove the head of the queue. It will return null if the queue is found empty.
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:
Java is a widespread p
About The Author:
Digital Marketing Course
₹ 29,499/-Included 18% GST
Buy Course₹ 41,299/-Included 18% GST
Buy Course