Updated June 21, 2023
Introduction to Java Queue Interface
Queue Interface is a member of the package util.java. It is a part of the collection framework & used to extend the collection interface. As the collection interface is the main interface, so queue interface includes all the methods of it. A queue is FIFO (First In First Out) data structure. Queue represents a data structure that is implemented in a way so that elements can be inserted into the last position in the order. In a queue, the item first inserted into the queue will be out first. A Queue in java is an interface; therefore, it can not be instantiated. Queue interface is implemented mainly in two classes, i.e. LinkedList & PriorityQueue. It is an ordered sequence of objects like a java list.
Syntax
In the following syntax, An object obj is instantiated using the LinkedList / PriorityQueue class. In the below two Queue syntax, LinkedList implementation is the standard one.
Queue qObj = new LinkedList();
OR
Queue qObj = new PriorityQueue();
Queue instances with the restricted data type can be created using the following given syntax.
Queue<String> q = new LinkedList<String>();
Queue Interface Methods
Some of the commonly used methods of queue interface are given below
- add(): add() method used to insert elements in the queue. It returns true if the response is successful; otherwise, it generates exceptions.
- element(): element() is one of the important methods that returns the queue head element. If the queue is empty, then it generates exceptions. In the example section, we can see how the element() method works.
- offer(): This method is also used to insert elements in the queue. The only difference between add() and the offer is that if the add method cannot add the element in the queue, it creates an exception while the offer method doesn’t.
- peek(): peek() method is almost similar to the element() method. It checks for the head of the queue. If the queue is empty, then it returns NULL
- poll(): This method removes an element from the front of the queue & returns the front element from the queue. If the queue is empty, then it returns null.
- remove(): This method used to remove & return the front element of the queue. If the element is found to be empty, it creates exceptions.
- size(): This method returns the count of the element available in the queue.
Examples to Implement Java Queue Interface
Below are the examples of implementing java queue interface:
Example #1
This example shows how different methods are used in the program & what these methods are returning.
- An instance of the Queue is created of type LinkedList.
- Further, different string elements are added in the queue by using the add method.
- In the next statement, the size of the queue is displayed using the size() method.
- Items in the queue, the head element of the queue are displayed in the next line.
- Remove method is used further for removing the element from the Queue.
- The next two lines display available items in the queue, head item of the queue, respectively.
Code:
//importing packages
import java.util.*;
public class QueueInterfaceExample {
public static void main(String[] args){
Queue qObj = new LinkedList();
//adding element to the queue
qObj.add("Action");
qObj.add("speak");
qObj.add("louder");
qObj.add("than");
qObj.add("Words");
//items available in the queue
System.out.println("\nTotal item count in Queue: " + qObj.size());
//printing queue here
System.out.println("\nItems in Queue: " + qObj);
//printing the head element of the queue
System.out.println("\nHead item of the Queue: " + qObj.element());
//removing head element from the queue
qObj.remove();
//items available in the queue
System.out.println("\nAvailable item in Queue: " + qObj);
//items available in the queue after applying peek method
System.out.println("\nHead item of the Queue: " + qObj.peek());
//applying the poll method to the
qObj.poll();
//items available in the queue after applying poll method
System.out.println("\nAvailable item in Queue: " + qObj);
}
}
Output:
Example #2
In this example, we can see how restricted types of elements can be added to the Queue.
Code:
//importing package here
import java.util.*;
public class QueueInterfaceExample2 {
public static void main(String[] args){
//initialize a Queue using a LinkedList
Queue<Integer> qObj = new LinkedList<>();
//adding element to the queue
qObj.add(50);
qObj.add(175);
qObj.add(1450);
qObj.add(2750);
qObj.add(10250);
//items available in the queue
System.out.println("\nTotal item count in Queue: " + qObj.size());
//printing queue here
System.out.println("\nItems in Queue: " + qObj);
//items available in the queue after applying poll method
System.out.println("\nAvailable item in Queue: " + qObj);
//declaring a integer variable here
Integer intVar = 1450;
//condition to check if element is available in the queue
if(qObj.contains(intVar)){
//items available in the queue after applying poll method
System.out.println("\nSpecified item is available in the Queue.");
}
//declaring a integer variable here
Integer intVar2 = 1500;
//condition to check if element is available in the queue
if(qObj.contains(intVar2)){
//items available in the queue after applying poll method
System.out.println("\nSpecified item is available in the Queue.");
}else{
//items available in the queue after applying poll method
System.out.println("\nSpecified item " + intVar2 + " is not available in the Queue.");
}
}
}
Output:
Example #3
In this example, trying to add the String type of element in the Integer type restricted Queue.
Code:
importing package here
import java.util.*;
public class QueueInterfaceExample3 {
public static void main(String[] args){
//initialize a Queue using a LinkedList
Queue<Integer> qObj = new LinkedList<>();
//adding element to the queue
qObj.add(50);
qObj.add(175);
qObj.add(1450);
qObj.add(2750);
qObj.add("Happy");
//items available in the queue
System.out.println("\nTotal item count in Queue: " + qObj.size());
//printing queue here
System.out.println("\nItems in Queue: " + qObj);
//items available in the queue after applying poll method
System.out.println("\nAvailable item in Queue: " + qObj);
}
}
Output:
The above-given program’s output will produce an error as inserting an element of String type in the Integer type Queue is not supported.
Conclusion
In the above-given article, Queue Interface is described clearly. It is used to extend the collection interfaces. It is also given how FIFO is used in the queue interface. The use of the queue interface is given in the above sections. Some of the examples are given in the article to see how the queue & queue method works.
Recommended Articles
This is a guide to Java Queue Interface. Here we discuss syntax, commonly used methods and examples of implementing a java queue interface with proper codes and outputs. You can also go through our other related articles to learn more –