Java Collections checkedQueue() Method with Examples

The checkedQueue() method in the Java Collections Framework is useful for making queues type safe at the runtime and therefore it is a very effective and important utility and it is for this reason that the checkedQueue() method is in the Collections class and can therefore be utilized as a way of creating a dynamic type safe view on a given queue. It means that any attempts to insert elements of the wrong type into the queue will cause a ClassCastException.

Method Signature

<E>: It is a generic type parameter which can act as a type and contain other parameters. The <E> describes that the method can work with any object of the type E, which is the type of elements in the queue.

Queue<E>: It is the return type of the method. It means that the method returns a Queue of type < E >.

checkedQueue: It refers to the name of the method.

Queue<E> queue: It is the first parameter of the method. It is a Queue that holds elements of type E. The method creates a type-safe view of this queue.

Class<E> type: It is the second parameter of the method. It is a Class object representing the type of elements in the queue. This parameter helps enforce type safety by ensuring that only elements of the specified type E can be added to the queue.

Example 1: Creating a type-safe view of the List

In this example, we create a regular Queue and use the Collections.checkedQueue() to wrap the queue, specifying the type of elements it should hold and add elements to the type-safe queue. Demonstrate what happens when an attempt is made to add an element of an incorrect type. Iterate Over the Queue of elements to show the correct usage.

Filename: CheckedQueueExample.java

Output

Caught a ClassCastException: Attempt to insert class java.lang.String element into collection with element type class Person
Alice (30)
Bob (25)
After adding another person:
Alice (30)
Bob (25)
Charlie (40)

Example 2: Basic Usage with Integer Queue

In this example, we will create a regular queue for Integer elements. Wrap the queue using Collections.checkedQueue() to ensure type safety. Add valid Integer elements to the queue. Attempt to add an invalid element to demonstrate the type safety provided by checkedQueue. Iterate through the queue to display the elements.

Filename: IntegerCheckedQueueExample.java

Output

Caught a ClassCastException: Attempt to insert class java.lang.String element into collection with element type class java.lang.Integer
1
2
3

Example 3: Custom Class with Additional Operations

In this example, we demonstrate the use of Collections.checkedQueue() with a custom class, Task, to enforce type safety and perform additional queue operations. The Task class represents a unit of work with a description and priority, this example includes adding tasks, processing tasks, and ensuring type safety when interacting with the queue.

Filename: TaskCheckedQueueExample.java

Output

Caught a ClassCastException: Attempt to insert class java.lang.String element into collection with element type class Task
Current tasks in the queue:
Task: Finish report, Priority: 1
Task: Email client, Priority: 2
Task: Prepare presentation, Priority: 3
Next task to process: Task: Finish report, Priority: 1
Tasks after adding more:
Task: Email client, Priority: 2
Task: Prepare presentation, Priority: 3
Task: Call supplier, Priority: 4
Task: Update website, Priority: 5
Next task to process: Task: Email client, Priority: 2
Remaining tasks in the queue:
Task: Prepare presentation, Priority: 3
Task: Call supplier, Priority: 4
Task: Update website, Priority: 5

Example 4: Mixed Operations with PriorityQueue

In this example, we will explore how to use Collections.checkedQueue() with a PriorityQueue. The PriorityQueue is a type of queue in Java that orders its elements based on their natural ordering or by a custom comparator provided at the time of queue construction. We will use a custom class, Task, to represent items in the queue and demonstrate various operations while ensuring type safety with Collections.checkedQueue().

Filename: TaskCheckedPriorityQueueExample.java

Output

Caught a ClassCastException: Attempt to insert class java.lang.String element into collection with element type class Task
Current tasks in the queue:
Task: Finish report, Priority: 1
Task: Email client, Priority: 2
Task: Prepare presentation, Priority: 3
Next task to process: Task: Finish report, Priority: 1
Tasks after adding more:
Task: Email client, Priority: 2
Task: Prepare presentation, Priority: 3
Task: Call supplier, Priority: 4
Task: Update website, Priority: 5
Next task to process: Task: Email client, Priority: 2
Remaining tasks in the queue:
Task: Prepare presentation, Priority: 3
Task: Update website, Priority: 5
Task: Call supplier, Priority: 4

Example 5: Handling Different Data Types with PriorityBlockingQueue

In this example, a PriorityBlockingQueue is created to manage tasks with different data types. The queue is wrapped with Collections.checkedQueue() to enforce type safety, ensuring only GenericTask objects can be added. Various tasks with different data types (String, Integer, Double) are added to the queue. The tasks in the queue are displayed and then processed in priority order and the processed tasks are displayed.

Filename: CheckedQueueGenericTaskExample.java

Output

Caught a ClassCastException: Attempt to insert class java.lang.String element into collection with element type class GenericTask
Tasks in the queue:
Task: Finish report, Priority: 1
Task: 123, Priority: 2
Task: 45.67, Priority: 3
Processing tasks in priority order:
Processed: Task: Finish report, Priority: 1
Processed: Task: 123, Priority: 2
Processed: Task: 45.67, Priority: 3

Applications

1. Input Validation in User Interfaces

When building graphical user interfaces (GUIs), user input often needs to be validated before processing. By using checkedQueue, you can ensure that only valid data types are accepted in input queues, preventing runtime errors.

2. Message Queues in Concurrent Systems

In multi-threaded applications where, multiple threads are producing and consuming messages, ensuring type safety in message queues is crucial. checkedQueue can help prevent data corruption or incorrect processing by enforcing type constraints.

3. Event Handling in GUI Frameworks

GUI frameworks often use event queues to manage user interactions and system events. By wrapping event queues with checkedQueue, you can ensure that only valid event objects are processed, reducing the risk of errors.

4. Task Scheduling in Job Queues

Job scheduling systems often use queues to manage tasks to be executed at specific times or intervals. checkedQueue can help enforce type safety in job queues, ensuring that only valid job objects are scheduled for execution.