Collections.checkedQueue(Queue, Class) in Java

Collections.checkedQueue(Queue, Class): This method is available in java.util.Collections class of Java.

Syntax:

<E> Queue<E> java.util.Collections.checkedQueue(Queue<E> queue, Class<E> type)

This method takes two arguments. This method returns a dynamically typesafe view of the specified queue.

Note: Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.

Parameters: Two parameters are required for this method.

queue: the queue for which a dynamically typesafe view is to be returned.

type: the type of element that the queue is permitted to hold.

Returns: a dynamically typesafe view of the specified queue.

Exceptions: NA

Approach

Java

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Queue;

public class CollectionscheckedQueue {
    public static void main(String[] args) {

        Queue<Integer> queue = new ArrayDeque<Integer>();
        queue.add(10);
        queue.add(12);
        queue.add(4);
        System.out.println(Collections.checkedQueue(queue,
Integer.class));

    }
}

Output:

[10, 12, 4]


No comments:

Post a Comment