Home Java Collections Methods checkedQueue() Java Collections checkedQueue() Method Definition In Java 8 and later, Collections.checkedQueue(queue, type) returns a Queue wrapper that type-checks elements passed to offer, add, and addAll. A wrong type throws ClassCastException before it ever enters the queue.
That matters for producer/consumer code: a bad item caught at the producer is easy to trace, while one that reaches the consumer fails somewhere unrelated.
A static method of the java.util.Collections utility class, available since Java 8. Call it on the class name, as in Collections.checkedQueue(...), after import java.util.Collections;. You never create a Collections object.
Syntax Pass the queue and the element class.
Queue<String> inbox = Collections.checkedQueue(new ArrayDeque<String>(), String.class);
Queue raw = inbox;
raw.offer(404); // ClassCastException Call What it does offer(x) / add(x) / addAll(c)Type-checked. poll() / peek() / remove()Behave exactly as on the wrapped queue. checkedQueue(new PriorityQueue<>(), T.class)Priority order is kept.
Added in Java 8; earlier versions had no checked wrapper for queues. Ordering (FIFO, priority, and so on) comes from the queue you wrap.
Library declarations // java.util.Collections (Java 8+)
public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type) Parameters Parameter Description queueThe queue to protect. typeThe class every element must be an instance of.
Return Value Return Value Description Queue<E>A dynamically type-safe view of queue.
Compare with Similar Methods Related methods:
Java Examples 1. Offer and poll 2. Filter at the gate 3. PriorityQueue Example 1 — A Type-Safe Message Inbox Offer two messages, reject an error code, then drain the queue.
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Queue;
public class CollectionsCheckedQueueInbox {
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(String[] args) {
Queue<String> inbox = Collections.checkedQueue(new ArrayDeque<String>(), String.class);
inbox.offer("hello");
inbox.add("bye");
Queue raw = inbox;
try {
raw.offer(404);
} catch (ClassCastException e) {
System.out.println("offer(404): " + e.getClass().getSimpleName());
}
System.out.println("peek " + inbox.peek() + ", size " + inbox.size());
System.out.println("poll " + inbox.poll() + ", " + inbox.poll() + ", then " + inbox.poll());
}
} offer(404): ClassCastException
peek hello, size 2
poll hello, bye, then null How It Works Both insert methods. offer and add are checked; the two strings get in.Rejected code. The Integer 404 fails the check, so the queue still has two messages.Normal FIFO. Reading is unaffected: hello, then bye, then null when empty.Example 2 — Bad Items Never Reach the Consumer A producer feeds mixed input; rejected items are counted, and the consumer sums only real integers.
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Queue;
public class CollectionsCheckedQueueGate {
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(String[] args) {
Queue<Integer> jobs = Collections.checkedQueue(new ArrayDeque<Integer>(), Integer.class);
Queue raw = jobs;
Object[] incoming = {5, "six", 7, 8.0, 9};
int rejected = 0;
for (Object o : incoming) {
try {
raw.add(o);
} catch (ClassCastException e) {
rejected++;
}
}
int sum = 0;
Integer j;
while ((j = jobs.poll()) != null) {
sum += j;
}
System.out.println("accepted sum " + sum + ", rejected " + rejected);
}
} accepted sum 21, rejected 2 How It Works Mixed input. The array holds three Integer values, a String, and a Double.Gatekeeper. "six" and 8.0 fail the check at add, so the producer can log or count them (2 here).Consumer stays simple. Every polled item really is an Integer, so unboxing never fails: 5 + 7 + 9 = 21.Example 3 — Wrap a PriorityQueue, and What Happens with null Priority order survives the wrapper; null passes the type check but the queue itself refuses it.
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;
public class CollectionsCheckedQueuePriority {
public static void main(String[] args) {
Queue<Integer> pq = Collections.checkedQueue(new PriorityQueue<Integer>(), Integer.class);
pq.addAll(Arrays.asList(30, 10, 20));
try {
pq.add(null);
} catch (NullPointerException e) {
System.out.println("add(null): " + e.getClass().getSimpleName());
}
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
sb.append(pq.poll()).append(' ');
}
System.out.println("poll order: " + sb.toString().trim());
}
} add(null): NullPointerException
poll order: 10 20 30 How It Works Checked batch. addAll checks all three numbers, then hands them to the PriorityQueue.null is the queue's call. The wrapper lets null through its type check, but PriorityQueue forbids null and throws NullPointerException.Priority kept. Polling returns the smallest first: 10, 20, 30.Frequently Asked Questions What does Collections.checkedQueue() do? It returns a Queue view that throws ClassCastException when offer, add, or addAll receives an element of the wrong type.
Which Java version added checkedQueue()? Java 8.
Does checkedQueue() change the queue order? No. FIFO, priority, or any other order comes from the wrapped queue.
Can I add null to a checked queue? The type check allows null, but most queues, including ArrayDeque and PriorityQueue, reject it with NullPointerException.
Why use checkedQueue() in producer/consumer code? It stops bad items at the producer, so the consumer never fails on an unexpected type.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion