- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections checkedQueue() Method
Description
The Java Collections checkedQueue(Queue<T>, Class <T>) method is used to get a dynamic typesafe view of a queue.
Declaration
Following is the declaration for java.util.Collections.checkedQueue() method.
public static <T> Queue<T> checkedQueue(Queue<T>, Class<T> type)
Type Parameters
T − This is the class of objects in queue.
Parameters
queue − This is the queue for which a dynamically typesafe view is to be returned.
type − This is the type of element that queue is permitted to hold.
Return Value
The method call returns a dynamically typesafe view of the specified queue.
Exception
NA
Getting a TypeSafe Queue from a Deque of Integers Example
The following example shows the usage of Java Collection checkedQueue(Deque) method to get a dynamically typesafe view of Queue. We've created a Deque object with some integers. Using checkedQueue(Deque) method, we get the queue then printed the queue.
package com.tutorialspoint; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.Queue; public class CollectionsDemo { public static void main(String[] args) { // create an array deque Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(20,30,20,30,15,22,11)); // get queue from the deque Queue<Integer> nq = Collections.checkedQueue(deque, Integer.class); System.out.println("View of the queue is: "+nq); } }
Output
Let us compile and run the above program, this will produce the following result.
View of the queue is: [20, 30, 20, 30, 15, 22, 11]
Getting a TypeSafe Queue from a Deque of Strings Example
The following example shows the usage of Java Collection checkedQueue(Deque) method to get a dynamically typesafe view of Queue. We've created a Deque object with some strings. Using checkedQueue(Deque) method, we get the queue then printed the queue.
package com.tutorialspoint; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.Queue; public class CollectionsDemo { public static void main(String[] args) { // create an array deque Deque<String> deque = new ArrayDeque<>(Arrays.asList("Welcome","to","Tutorialspoint")); // get queue from the deque Queue<String> nq = Collections.checkedQueue(deque, String.class); System.out.println("View of the queue is: "+nq); } }
Output
Let us compile and run the above program, this will produce the following result.
View of the queue is: [Welcome, to, Tutorialspoint]
Getting a TypeSafe Queue from a Deque of Objects Example
The following example shows the usage of Java Collection checkedQueue(Deque) method to get a dynamically typesafe view of Queue. We've created a Deque object with some student objects. Using checkedQueue(Deque) method, we get the queue then printed the queue.
package com.tutorialspoint; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.Queue; public class CollectionsDemo { public static void main(String[] args) { // create an array deque Deque<Student> deque = new ArrayDeque<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam"))); // get queue from the deque Queue<Student> nq = Collections.checkedQueue(deque, Student.class); System.out.println("View of the queue is: "+nq); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result.
View of the queue is: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
To Continue Learning Please Login
Login with Google