Queue Interface
The Queue interface has several sub interfaces and implementations that cater to different use cases, such as Deque, Blocking Queue, and Transfer Queue.
Basic Queue Operations
- Adding elements: offer(E e) – inserts the specified element into the queue.
- Removing elements: poll() – retrieves and removes the head of the queue, returning null if the queue is empty.
- Accessing elements: peek() – retrieves, but does not remove, the head of the queue, returning null if the queue is empty.
LinkedList
The LinkedList class implements both the List and Deque interfaces, making it a versatile choice for both list and queue operations.
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
// Example using soccer player names
Queue<String> soccerPlayers = new LinkedList<>();
// Adding elements
soccerPlayers.offer("Lionel Messi");
soccerPlayers.offer("Cristiano Ronaldo");
soccerPlayers.offer("Neymar Jr");
// Accessing the head element
System.out.println("Head player: " + soccerPlayers.peek()); // Output: Lionel Messi
// Removing elements
soccerPlayers.poll();
System.out.println("After poll: " + soccerPlayers); // Output: [Cristiano Ronaldo, Neymar Jr]
// Iterating over elements
for (String player : soccerPlayers) {
System.out.println("Player: " + player);
}
}
}
PriorityQueue
The PriorityQueue class provides an implementation of the Queue interface that orders its elements according to their natural ordering or by a Comparator provided at queue construction time.
import java.util.PriorityQueue;
import java.util.Queue;
public class PriorityQueueExample {
public static void main(String[] args) {
// Example using tennis player names with priority based on name length
Queue<String> tennisPlayers = new PriorityQueue<>((a, b) -> a.length() - b.length());
// Adding elements
tennisPlayers.offer("Roger Federer");
tennisPlayers.offer("Rafael Nadal");
tennisPlayers.offer("Novak Djokovic");
// Accessing the head element
System.out.println("Head player: " + tennisPlayers.peek()); // Output: Rafael Nadal
// Removing elements
tennisPlayers.poll();
System.out.println("After poll: " + tennisPlayers); // Output: [Novak Djokovic, Roger Federer]
// Iterating over elements
for (String player : tennisPlayers) {
System.out.println("Player: " + player);
}
}
}
ArrayDeque
The ArrayDeque class is a resizable array implementation of the Deque interface and can be used both as a queue (FIFO) and as a stack (LIFO).
import java.util.ArrayDeque;
import java.util.Queue;
public class ArrayDequeExample {
public static void main(String[] args) {
// Example using cricket player names
Queue<String> cricketPlayers = new ArrayDeque<>();
// Adding elements
cricketPlayers.offer("Virat Kohli");
cricketPlayers.offer("Steve Smith");
cricketPlayers.offer("Kane Williamson");
// Accessing the head element
System.out.println("Head player: " + cricketPlayers.peek()); // Output: Virat Kohli
// Removing elements
cricketPlayers.poll();
System.out.println("After poll: " + cricketPlayers); // Output: [Steve Smith, Kane Williamson]
// Iterating over elements
for (String player : cricketPlayers) {
System.out.println("Player: " + player);
}
}
}
Conclusion
While the Queue interface is not part of the List hierarchy, it is an essential part of the Java Collections Framework, providing a different set of functionalities suited for holding and processing elements in a specific order. Various implementations of Queue offer different behaviors and performance characteristics, making it a versatile tool for developers.