105
Java Collections checkedNavigableSet() Method
The checkedNavigableSet() is an inbuilt method of Java Collections class. This method is used to get a dynamically typesafe view of the specified Navigable Set. If any attempt of inserting an element whose value has the wrong type, it will through an immediate ClassCastException.
Syntax
Following is the declaration of checkedNavigableSet() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
s | It is the navigable set for which a dynamically typesafe view is to be returned. | Required |
type | It is the type of element that s is permitted to hold. | Required |
Returns
The checkedNavigableSet() method returns a dynamically typesafe view of the specified Navigable Set.
Exceptions
ClassCastException
Compatibility Version
Java 1.8 and above
Example 1
Output:
Type safe view of the Navigable Set is: [A1, B2, C3, D4]
Example 2
Output:
The view of the Navigable Set is: [11, 22, 33, 44]
Example 3
Output:
Type safe view of the Navigable Set1 is: [A, B] Type safe view of the Navigable Set2 is: [11, 12] Exception in thread "main" java.lang.ClassCastException: java.base/java.lang.Integer cannot be cast to java.base/java.lang.String at java.base/java.lang.String.compareTo(String.java:124) at java.base/java.util.TreeMap.put(TreeMap.java:566) at java.base/java.util.TreeSet.add(TreeSet.java:255) at myPackage.CollectionCheckedNavigableSetExample3.main(CollectionCheckedNavigableSetExample3.java:21)
Next TopicJava Collections Class