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 ]]
java_util_collections.htm
Advertisements