Java Queue Interface
The Queue interface is a child interface of the Collection interface. The Queue interface is available inside the java.util package. It defines the methods that are commonly used by classes like PriorityQueue and ArrayDeque.
The Queue is used to organize a sequence of elements prior to the actual operation.
🔔 The Queue interface extends Collection interface.
🔔 The Queue interface allows duplicate elements.
🔔 The Queue interface preserves the order of insertion.
The Queue interface defines the following methods.
Let's consider an example program on PriorityQueue to illustrate the methods of Queue interface.
Example
import java.util.*;
public class QueueInterfaceExample {
public static void main(String[] args) {
Queue queue = new PriorityQueue();
queue.offer(10);
queue.offer(20);
queue.offer(30);
queue.offer(40);
System.out.println("\nQueue elements are - " + queue);
System.out.println("\nHead element - " + queue.element());
System.out.println("\nHead element again - " + queue.peek());
queue.remove();
System.out.println("\nElements after Head removal - " + queue);
queue.poll();
System.out.println("\nElements after one more Head removal - " + queue);
}
}
When we run this code, it produce the following output.