The Java queue is a Java Collection subclass of an interface. Much like a Java array, it portrays an ordered sequence of entities, but its targeted use has been subtly different. Since the queue java interface is a child type or some subclass type of an interface from Java Collection, the Queue interface also contains all of the mechanisms in the Collection interface.
The java queue interface, java.util.Queue, is a data design or the structure constructed to add components at the last space of the queue and to delete items from the starting of some queue. This queue in java is equivalent to how a line functions in a store.
The java queue uses a FIFO mechanism to access the components.
As an interface, the queue requires a particular statement class and the Linked List and java priority queue are perhaps the most general classes.
Table of Contents
How the Java queue is utilized?
To utilize the java queue in any code, it is mandatory to import the package named java.util.Queue. The illustration is displayed below:
import java.util.Queue;
// Below displayed is linked list implementation of some Queue
Queue<String> fruit_1 = new LinkedList<>();
// Below displayed is an array implementation of some queue
Queue<String> fruit_2 = new ArrayDeque<>();
// Below displayed is an implementation of a priority queue
Queue<String> fruit_3 = new PriorityQueue<>();
Unique types of Java queue
There are 3 unique types of java queue. They are represented below:
- Priority Queue
- Linked List
- Array Deque
The priority queue java is generally used to implement the code.
Various methods are utilized in the Java queue
1. Boolean add(item)
It is utilized to inject the specified item into some java queue, and thus, this method is often used to display true upon completion.
2. Boolean offer(item)
It is utilized to add the specified element into some queue.
3. Object remove()
It is utilized to remove or cut off the head of some queue and then retrieve it.
4. Object item()
It is utilized to fetch the head of some java queue but doesn’t ever delete or remove it.
Casual syntax of Java queue
The casual syntax for a linked list and priority queue java is displayed as:
Queue queue_1 = new LinkedList();
Queue queue_2 = new PriorityQueue();
Java Queue Example
import java.util.Queue;
import java.util.PriorityQueue;
class demo_1 {
public static void main(String args[]) {
// let’s create priority queue
Queue<Integer> num = new PriorityQueue<>();
// let’s offer some integer elements to priority queue
num.offer(8);
num.offer(3);
num.offer(6);
System.out.println("Numbers currently exist in queue are: " + num);
// let’s access the items which is present in the priority queue
int acc_num = num.peek();
System.out.println("The specified values that are accessed in queue are: " + acc_num);
// let’s remove or delete items from the priority queue
int rem_num = num.poll();
System.out.println("Removed items are: " + rem_num);
System.out.println("New queue is: " + num);
}
}
//Output of the illustration is:
// Numbers currently exist in queue are: [8,3,6]
// The specified values that are accessed in queue are: 6
// Removed items are: 6
// New queue is: [8,3]
Another illustration for java queue is displayed as below:
import java.util.*;
class demo_2 {
public static void main(String args[]) {
// let’s create one linked list which is empty
Queue<Integer> num = new LinkedList<Integer>();
// let’s add some integer elements to the empty linked list
num.add(4);
num.add(9);
num.add(2);
System.out.println("Numbers currently added in linked list are: " + num);
// let’s print and display the top most item that is present in the linked list
System.out.println(num.peek());
// let’s remove the top most item from the linked list
System.out.println(num.poll());
// let’s print or display the top most item present in the new or updated linked list
System.out.println(num.peek());
}
}
//Output of the illustration is:
//Numbers currently added in linked list are: [4,9,2]
//4
//4
//9