Java Collections checkedNavigableSet() Method with Examples
Last Updated :
03 Jan, 2022
The checkedQueue() method of Java Collections is a method that returns a dynamically and typesafe view of the given Set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.
Syntax:
public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> set, Class<E> datatype)
Parameters:
- set is an input set data
- datatype is the type of elements that set can hold
Return Type: This method will return the dynamically and typesafe view of the given Set.
Exceptions:
- ClassCastException: ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another.
Example 1:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
NavigableSet<String> data = new TreeSet<>();
data.add( "java" );
data.add( "php/jsp" );
data.add( "python" );
data.add( "R" );
System.out.println(Collections.checkedNavigableSet(
data, String. class ));
}
}
|
Output
[R, java, php/jsp, python]
Example 2:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
NavigableSet<Integer> data = new TreeSet<>();
data.add( 1 );
data.add( 2 );
data.add( 3 );
data.add( 4 );
System.out.println(Collections.checkedNavigableSet(
data, Integer. class ));
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...