Collections.checkedNavigableSet(NavigableSet, Class) in Java

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

Syntax:

<E> NavigableSet<E> java.util.Collections.checkedNavigableSet(NavigableSet<E> s, Class<E> type)

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

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.

s: the navigable set for which a dynamically typesafe view is to be returned.

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

Returns: a dynamically typesafe view of the specified navigable set.

Exceptions: NA

Approach

Java

import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

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

        NavigableSet<String> set = new TreeSet<String>();
        set.add("Hello");
        set.add("Java");
        set.add("Hello");
        System.out.println(Collections.checkedNavigableSet(set,
String.class));
    }
}

Output:

[Hello, Java]


No comments:

Post a Comment