Java Queue Interface
In this tutorial, we will learn about the java.util.Queue interface and explanation of each method along with the working examples of Queue interface.
java.util.Queue
java.util.Queue
is an interface of collections framework, as it is an interface do not have any concrete implementation of its methods but sets guide lines to the classes which implements it.
As the name suggest and follows queue mechanism of the elements added to it, which is first-in and first-out(FIFO).
Queue interface is mainly used in the tasks like scheduling, messaging systems and online booking services etc.
Queue interface methods
Following are the methods of Queue, and it's important to understand each of these methods.
Method | Description |
---|---|
boolean add(E element) | Adds given element in to the queue, Insertion is successful, returns true . If insertion is not possible due to capacity restrictions throws an il legalStateException . |
boolean offer(E element) | Adds given element in to the queue, if it is successful returns true and returns false for failed cases. |
E remove() | Retrieves and removes the head of the queue. If the queue is empty will throws an NoSuchElementException . |
E poll() | Retrieves and removes the head of the queue. If the queue is empty will returns null |
E element() | Returns the head of the queue but will not remove head of the queue. Throws a NoSuchElementException if the queue is empty. |
E peek() | Retrieves, but does not remove, the head of the queue. If the queue is empty will returns null . |
Queue interface example 1:
import java.util.LinkedList;
import java.util.Queue;
public class MyQueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Add elements to the queue
queue.add("element1");
queue.add("element2");
queue.add("element3");
// Remove and print the elements from the queue
while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}
}
Output:
element1
element2
element3
Explanation:
In the above example of Queue
, we create a new instance of the LinkedList
class and assign it to the queue
variable of type Queue.
LinkedList
also implements the Queue
interface not just List. Then we add three elements to the queue using the add
method and remove them one by one using the remove
method until the queue is empty.
Queue interface example 2:
In this example, we will use other class which implemented the Queue interface.
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new PriorityQueue();
queue.offer("first");
queue.offer("second");
queue.offer("third");
queue.offer("fourth");
queue.add("fifth");
queue.offer("sixth");
System.out.println(queue);
while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}
}
Output:
[fifth, first, sixth, second, fourth, third]
fifth
first
fourth
second
sixth
third
Use Cases:
Queue implementations will be used in many application, few of them as following.
- Online ticket bookings.
- Messaging systems.
- Scheduled Tasks.
- Computer networking applications.
Conclusion:
In this tutorial, we have covered java.util.Queue interface, its methods and use cases along with the working examples of it.