Java Collections checkedQueue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
Queue<String> inbox = Collections.checkedQueue(new ArrayDeque<String>(), String.class);
Queue raw = inbox;
raw.offer(404);   // ClassCastException
CallWhat 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
// java.util.Collections (Java 8+)
public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type)

Parameters

ParameterDescription
queueThe queue to protect.
typeThe class every element must be an instance of.

Return Value

Return ValueDescription
Queue<E>A dynamically type-safe view of queue.

Compare with Similar Methods

Related methods:

MethodHow it differs
checkedCollection(c, type)General version; returns only Collection.
asLifoQueue(deque)Another Queue adapter, for LIFO order.
ArrayDequeA common queue to wrap.

Java Examples

Example 1 — A Type-Safe Message Inbox

Offer two messages, reject an error code, then drain the queue.

Java
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());
    }
}

How It Works

  1. Both insert methods. offer and add are checked; the two strings get in.
  2. Rejected code. The Integer 404 fails the check, so the queue still has two messages.
  3. 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.

Java
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);
    }
}

How It Works

  1. Mixed input. The array holds three Integer values, a String, and a Double.
  2. Gatekeeper. "six" and 8.0 fail the check at add, so the producer can log or count them (2 here).
  3. 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.

Java
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());
    }
}

How It Works

  1. Checked batch. addAll checks all three numbers, then hands them to the PriorityQueue.
  2. null is the queue's call. The wrapper lets null through its type check, but PriorityQueue forbids null and throws NullPointerException.
  3. Priority kept. Polling returns the smallest first: 10, 20, 30.

Frequently Asked Questions

It returns a Queue view that throws ClassCastException when offer, add, or addAll receives an element of the wrong type.
Java 8.
No. FIFO, priority, or any other order comes from the wrapped queue.
The type check allows null, but most queues, including ArrayDeque and PriorityQueue, reject it with NullPointerException.
It stops bad items at the producer, so the consumer never fails on an unexpected type.

Next: checkedSet()

Wrap a Set so adding the wrong element type fails immediately.

checkedSet() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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