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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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>();
Some of the commonly used methods of queue interface are given below
Below are the examples of implementing java queue interface:
This example shows how different methods are used in the program & what these methods are returning.
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:
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:
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.
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.
The above is the detailed content of Java Queue Interface. For more information, please follow other related articles on the PHP Chinese website!