Collections.checkedSortedSet(SortedSet, Class) in Java

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

Syntax:

<E> SortedSet<E> java.util.Collections.checkedSortedSet(SortedSet<E> s, Class<E> type)

This method takes two arguments. This method returns a dynamically typesafe view of the specified sorted 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 sorted 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 sorted set.

Exceptions: NA

Approach

Java

import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;

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

        SortedSet<String> sortedSet = new TreeSet<String>();
        sortedSet.add("Hello");
        sortedSet.add("Java");
        sortedSet.add("Abc");
        sortedSet.add("Hello");
        System.out.println(Collections.checkedSortedSet(
sortedSet, String.class));

    }
}

Output:

[Abc, Hello, Java]


No comments:

Post a Comment