The Queue interface in Java’s Collections framework provides functionalities for a typical FIFO (first-in-first-out) data structure. This means that the element that is inserted first is the first one to be removed.
Key Points:
Nature: Queue is a FIFO (First-In-First-Out) data structure. The first element added is the first to be removed.
Interface: Queue is an interface in Java’s Collections framework.
Implementations: Common implementations include LinkedList (doubles as a List and Queue) and PriorityQueue.
Core Methods:
- add(E e): Add an element. Throws an exception if it fails.
- offer(E e): Add an element. Returns false if it fails.
- remove(): Remove and retrieve the head. Throws an exception if the queue is empty.
- poll(): Remove and retrieve the head. Returns null if the queue is empty.
- element(): Retrieve (but don’t remove) the head. Throws an exception if empty.
- peek(): Retrieve (but don’t remove) the head. Returns null if empty.
Variations: Besides basic queues, Java has Deque (double-ended queue) allowing insertions/removals at both ends, and PriorityQueue which orders elements by natural ordering or a comparator.
Thread Safety: Queue implementations like LinkedList and PriorityQueue are not thread-safe. For concurrent scenarios, use classes like ConcurrentLinkedQueue or ArrayBlockingQueue from the java.util.concurrent package.
Basic Operations on a Queue
import java.util.LinkedList;
import java.util.Queue;
public class QueueOperations {
public static void main(String[] args) {
// 1. Creating a Queue
Queue<Integer> queue = new LinkedList<>();
// 2. Adding elements to the Queue
queue.offer(10);
queue.offer(20);
queue.offer(30);
queue.offer(40);
System.out.println("Queue after adding 10, 20, 30, 40: " + queue);
// 3. Removing an element from the front of the Queue
int removed = queue.poll();
System.out.println("Removed element: " + removed);
// 4. Peeking (looking at the front without removing)
int front = queue.peek();
System.out.println("Element at the front: " + front);
// 5. Checking if the Queue is empty
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty? " + isEmpty);
// 6. Finding the size of the Queue
int size = queue.size();
System.out.println("Size of the queue: " + size);
}
}
Output:
Queue after adding 10, 20, 30, 40: [10, 20, 30, 40] Removed element: 10 Element at the front: 20 Is the queue empty? false Size of the queue: 3
Explanation:
1. A Queue of type Integer is created using a LinkedList.
2. Four integers (10, 20, 30, and 40) are added to the queue using the offer method.
3. The poll() method is used to remove the element at the front of the queue.
4. The peek() method is used to view the front element without removing it.
5. The isEmpty() method is used to check if the queue is empty.
6. The size() method is used to determine the number of elements in the queue.
Iterating over a Queue in Java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Iterator;
public class QueueIteration {
public static void main(String[] args) {
// Creating a Queue
Queue<String> queue = new LinkedList<>();
queue.offer("A");
queue.offer("B");
queue.offer("C");
queue.offer("D");
// 1. Iterate using Java 8 forEach() method
System.out.print("Using Java 8 forEach(): ");
queue.forEach(item -> System.out.print(item + " "));
System.out.println();
// 2. Iterate using iterator()
System.out.print("Using iterator(): ");
Iterator<String> iterator = queue.iterator();
while(iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
// 3. Iterate using iterator() and Java 8 forEachRemaining() method
System.out.print("Using iterator() with forEachRemaining(): ");
iterator = queue.iterator();
iterator.forEachRemaining(item -> System.out.print(item + " "));
System.out.println();
// 4. Iterate using simple for-each loop
System.out.print("Using simple for-each loop: ");
for(String item : queue) {
System.out.print(item + " ");
}
}
}
Output:
Using Java 8 forEach(): A B C D Using iterator(): A B C D Using iterator() with forEachRemaining(): A B C D Using simple for-each loop: A B C D
Explanation:
1. The Queue is initialized with four elements (A, B, C, D).
2. The Java 8 forEach() method is used to iterate over and print each element.
3. The iterator() method of the Queue is used in combination with a while loop to iterate over and print each element.
4. The iterator() method, combined with Java 8's forEachRemaining() method, is used to iterate over and print each element.
5. A simple for-each loop is used to iterate over and print each element.