94
Java Collections checkedSet() Method
The checkedSet() is an inbuilt method of Java Collections class. This method is used to get a dynamically typesafe view of the specified Set.
Syntax
Following is the declaration of checkedSet() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
s | It is the 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 checkedSet() method automatically returns a dynamically typesafe view of the specified Set in the ascending order.
Exceptions
ClassCastException
Compatibility Version
Java 1.5 and above
Example 1
Output:
Type safe view of the Set is: [Hindi100, TutorAspire , JavaTraining, SSSIT]
Example 2
Output:
The view of the Set is: [500, 900, 1100, 2200]
Example 3
Output:
Dynamic type safe view of Set is: [1800, 1500, 1100] Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.String element into collection with element type class java.lang.Integer at java.base/java.util.Collections$CheckedCollection.typeCheck(Collections.java:3038) at java.base/java.util.Collections$CheckedCollection.add(Collections.java:3081) at myPackage.CollectionCheckedSetExample3.main(CollectionCheckedSetExample3.java:12)
Next TopicJava Collections Class