Java Collections Essentials — Part 3
There are three components that extend the collection interface i.e List, Queue and Sets. Let’s discuss about Queue.
Queue is an interface in Java that represents a collection designed for holding elements prior to processing. There are two main types of Queue implementations mentioned:
- PriorityQueue
- ArrayDeque
PriorityQueue
PriorityQueue<E> pq = new PriorityQueue<>();
- A queue that orders elements based on their priority
Best for: Scenarios where elements need to be processed based on their priority, such as task scheduling or CPU scheduling
PriorityQueue orders elements based on their priority, allowing the highest-priority element to be retrieved quickly. This makes it ideal for situations where tasks or items need to be processed in order of importance.
Limitations: Limited access to elements (mainly head)
PriorityQueue primarily allows access to the head (highest priority) element. Accessing other elements in the queue is not as efficient, as the internal structure is optimized for retrieving the highest priority item.
Main algorithms:
- add(E e) or offer(E e): Inserts an element
- poll(): Retrieves and removes the head of the queue
- peek(): Retrieves but does not remove the head of the queue
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
// add() or offer(): Insert elements
pq.add(3);
pq.offer(1);
pq.add(4);
pq.offer(2);
System.out.println("PriorityQueue: " + pq);
// peek(): Retrieve the head element without removing
System.out.println("Head element: " + pq.peek());
// poll(): Retrieve and remove the head element
System.out.println("Removed element: " + pq.poll());
System.out.println("Updated PriorityQueue: " + pq);
// contains(): Check if an element exists
System.out.println("Contains 3? " + pq.contains(3));
// Iterate through the queue
System.out.println("Iterating through the queue:");
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
}
}
ArrayDeque
ArrayDeque<E> ad = new ArrayDeque<>();
- A resizable array implementation of Deque (double-ended queue)
Best for: Scenarios requiring efficient insertion and removal at both ends, such as implementing both stack and queue behaviors
Limitations:
Both types generally offer limited access to elements, mainly to the head (and tail for ArrayDeque)
Main algorithms:
- addFirst(E e) / offerFirst(E e): Inserts at the front
- addLast(E e) / offerLast(E e): Inserts at the end
- removeFirst() / pollFirst(): Removes from the front
- removeLast() / pollLast(): Removes from the end
import java.util.ArrayDeque;
public class ArrayDequeDemo {
public static void main(String[] args) {
ArrayDeque<String> deque = new ArrayDeque<>();
// addFirst() / offerFirst(): Insert at the front
deque.addFirst("First");
deque.offerFirst("Very First");
// addLast() / offerLast(): Insert at the end
deque.addLast("Last");
deque.offerLast("Very Last");
System.out.println("ArrayDeque: " + deque);
// getFirst() / peekFirst(): Retrieve the first element
System.out.println("First element: " + deque.getFirst());
// getLast() / peekLast(): Retrieve the last element
System.out.println("Last element: " + deque.getLast());
// removeFirst() / pollFirst(): Remove from the front
System.out.println("Removed first: " + deque.removeFirst());
// removeLast() / pollLast(): Remove from the end
System.out.println("Removed last: " + deque.removeLast());
System.out.println("Updated ArrayDeque: " + deque);
// contains(): Check if an element exists
System.out.println("Contains 'First'? " + deque.contains("First"));
// Iterate through the deque
System.out.println("Iterating through the deque:");
for (String item : deque) {
System.out.println(item);
}
}
}
A detailed breakdown of Sets collection type will be covered in Java Collections Essentials — Part 4