Introduction to Queue in Java
The queue is a data structure that works on the First In First Out (FIFO) principle. It is used to hold the object to be processed in order of their arrival; this is very similar to the line of people standing in a queue. As Java provides large support for data structure in the form of the Collection interface, the Queue is an interface that is available in the Collection interface. It extends the Collection interface. It is available in java.util package and supports all the operations available in the Collection interface along with some additional extraction, insertion, and inspection operations.
Syntax:
Interface Queue<E>
As Queue is an interface, not a class, it cannot be instantiated directly. From the declaration, it can be seen that the Queue is accepting values as generic similar to collections, and we can pass any type of object to it. Java has multiple implementations of the Queue interface, which we can use while using the Queues. They are LinkedList and PriorityQueue.
A queue can be declared as below:
Queue< Object > q = new LinkedList<>();
Queue< Object > q = new PriorityQueue<>();
How does Queue work in Java?
- As stated earlier, the Queue interface extends the collection interface; hence, it supports all basic operational methods available in it.
- The implementations such as LinkedList, PriorityQueue implements the queue-specific methods, which are declared in the Queue interface.
- The LinkedList holds the elements as the standard of LinkedList, i.e. in insertion order. The PriorityQueue whereas maintains the natural order of inserted elements.
- Note that these two implementations are not thread-safe, and for this, Java provides another implementation named PriorityBlockingQueue, which is threaded safe.
Queue Member Types in Java
- As the queue is an interface, it contains all abstract methods only and not any data members.
- The Queue provides only a way to define the operations, and these are implemented in child classes.
Queue Functions in Java
- As the queue supports the FIFO structure, it allows the insert of the elements from one end and removes the elements from another (front) end.
- These are the two basic operations that are supported by a queue.
- All the methods available in a queue can be divided into two categories; the first type of method throws an exception in case of failure of operation such as no element found, and in the second type of methods instead of exception any specific value such as null or false is returned in case of operation failure.
- There is a head in a queue concept that always represents the first element in the queue; upon removal, this head element will be removed first.
Below are the all methods available in Queue:
Returns special value | Throws exception | |
Insert | offer(e) | add(e) |
Remove | poll() | remove() |
Examine | peek() | element() |
So as explained, there are two types of methods that throw an exception and which return special value. There are three types of operation in this kind of operation: an insertion, the second is removal, and the third is retrieval or examine. In the case of remove operation, an object will be removed actually from the queue but in the case of examine the object will be returned without actually removing it from the queue.
Examples of Queue in Java
Given below are the different examples of Queue in Java:
Example #1 – Add operation with LinkedList implementation
Code:
import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
}
}
Output:
Note here that the order of insertion is the same with output from left to write.
Example #2 – Let’s remove the added elements one by one
Code:
import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}
System.out.println("");
System.out.println(q);
}
}
Output:
Here, we have used function isEmpty() to check when the queue becomes empty after the removal of elements. The removal order is the same as per the insertion. We have printed the queue after the removal of all the elements; that’s why we are getting an empty bracket in the end.
Example #3 – Insertion and Removal Operation on PriorityQueue
Code:
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}
System.out.println("");
System.out.println(q);
}
}
Output:
Here, we have used PriorityQueue, which will hold and return the elements depending upon the natural ordering of elements or upon the comparator, if any passed. Note the insertion order and removal order is not the same. The removal is based totally on the value of elements.
Example #4 – Examine operation on LinkedList
Code:
import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
System.out.println( q.peek() );
System.out.println(q);
}
}
Output:
Note here that we have used the peek() function, which will return the head of the queue without actually removing it. We have printed the queue after the peek operation, and it can be seen that the head element, i.e. 5, is not removed from the queue.
Example #5 – Examine operation on PriorityQueue
Code:
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer> ();
q.add(5);
q.add(2);
q.add(1);
q.add(4);
q.add(3);
System.out.println(q);
System.out.println( q.peek() );
System.out.println(q);
}
}
Output:
This is similar to the previous example’s LinkedList operation, but note the head element is 1 because it’s a PriorityQueue.
Conclusion
The Queue is a special interface in Java that is used to hold the elements in insertion order. It supports operations like insertion, retrieval, and removal. There are alternative methods available for all the methods. We have seen examples of the most commonly used methods in queue operation.
Recommended Articles
This is a guide to Queue in Java. Here we discuss the introduction, functions, working, and different examples of the queue in java. You may also have a look at the following articles to learn more –