Java Collections checkedQueue() Method

The checkedQueue() is a method of Java Collections class which returns a dynamically typesafe view of the specified Queue. If an element inserted of the wrong type, it will result in an immediate ClassCastException.

Syntax

Following is the declaration of checkedQueue() method:

Parameter

ParameterDescriptionRequired/Optional
queueIt is the Queue for which a dynamically typesafe view is to be returned.Required
typeIt is the type of element that Queue is permitted to hold.Required

Returns

The checkedQueue() method returns a dynamically typesafe view of the specified Queue.

Exceptions

ClassCastException

Compatibility Version

Java 1.8 and above

Example 1

Test it Now

Output:

Type safe view of the Queue is: [A, B, C, D]

Example 2

Test it Now

Output:

Type safe view of the Queue is: [55, 66, 66, 77, 88, 77]

Example 3

Test it Now

Output:

[1, 2, 5]
Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.String element into collection with element type class java.lang.Integer
	at java.base/java.util.Collections$CheckedCollection.typeCheck(Collections.java:3038)
	at java.base/java.util.Collections$CheckedQueue.offer(Collections.java:3188)
	at myPackage.CollectionCheckedQueueExample3.main(CollectionCheckedQueueExample3.java:12)





Latest Courses