VOOZH about

URL: https://www.geeksforgeeks.org/java/java-collections-checkedqueue-method-with-examples/

⇱ Java Collections checkedQueue() Method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Collections checkedQueue() Method with Examples

Last Updated : 3 Jan, 2022

The checkedQueue() method of Java Collections is a method that returns a dynamically and typesafe view of the given Queue. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.

Syntax:

public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type) 

Parameters:

  • queue is the queue that is returned for dynamically safe
  • type is the data type of the queue elements

Return Type: This method will return the dynamically and typesafe view of the given Queue.

Exceptions:

  • ClassCastException: ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another.

Example 1: Create a type-safe view of the List using checkedQueue() Method


Output
[C, Java/jsp, Python, R]

Example 2:


Output
[1, 21, 56, 23]
Comment