The Collections.checkedQueue() method in Java returns a dynamically type-safe view of the specified Queue. It ensures that only elements of the specified type can be added to the queue. If an element of an incompatible type is inserted, the method throws a ClassCastException at runtime.
- Useful when working with legacy code or raw collections.
- Changes made through the checked queue are reflected in the original queue.
import java.util.*;
public class CheckedQueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
Queue<String> checkedQueue =
Collections.checkedQueue(queue, String.class);
checkedQueue.offer("Java");
checkedQueue.offer("Python");
checkedQueue.offer("C++");
System.out.println(checkedQueue);
}
}
Output
[Java, Python, C++]
Explanation: This example creates a LinkedList queue and wraps it using checkedQueue(). The returned queue allows only String elements, ensuring runtime type safety.
Syntax
public static <E> Queue<E> checkedQueue(
Queue<E> queue,
Class<E> type)
Parameters:
- queue: The queue for which a type-safe view is created.
- type: The
Classobject representing the permitted element type.
Return Type: Returns a dynamically type-safe view of the specified queue.
Exceptions: ClassCastException: Thrown if an element of an incompatible type is inserted into the checked queue.
Example: Create a type-safe view of the List using checkedQueue() Method
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create a queue
Queue<String> data = new PriorityQueue<String>();
// add elements
data.add("Python");
data.add("R");
data.add("C");
data.add("Java/jsp");
// Create type safe view of the List
System.out.println(
Collections.checkedQueue(data, String.class));
}
}
Output
[C, Java/jsp, Python, R]
Explanation: This example creates a PriorityQueue of String elements and wraps it using Collections.checkedQueue(). The returned queue accepts only String values and displays them according to the queue's ordering.
Example: Creating a Type-Safe Queue of Integers
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create a queue
Queue<Integer> data = new PriorityQueue<Integer>();
// add elements
data.add(1);
data.add(23);
data.add(56);
data.add(21);
// Create type safe view of the List
System.out.println(
Collections.checkedQueue(data, Integer.class));
}
}
Output
[1, 21, 56, 23]
Explanation: This example creates a PriorityQueue of Integer elements and returns a runtime type-safe view using Collections.checkedQueue(). Since all inserted elements are integers, the queue is displayed successfully.
checkedQueue() vs checkedCollection()
| Feature | checkedQueue() | checkedCollection() |
|---|---|---|
| Works with | Queue | Any Collection |
Queue operations (offer(), poll(), peek()) | Supported | Not available |
| Runtime type checking | Yes | Yes |
| Preserves queue behavior | Yes | No queue-specific operations |
Advantages
- Ensures runtime type safety for queue elements.
- Prevents insertion of incompatible element types.
- Helps detect programming errors early.
- Useful with legacy APIs and raw collections.
- Preserves all operations of the original queue.