Javatpoint Logo
Javatpoint Logo

Java Collections checkedCollection() Method

The checkedCollection() is a method of Java Collections class which returns a dynamically typesafe view of the specified collection. If an element inserted of the wrong type, it will result in an immediate ClassCastException.

Syntax

Following is the declaration of checkedCollection() method:

Parameter

Parameter Description Required/Optional
c It is the collection for which a dynamically typesafe view is to be returned. Required
type It is the type of element that c is permitted to hold. Required

Returns

The checkedCollection() method returns a dynamically typesafe view of the specified collection.

Exceptions

ClassCastException

Compatibility Version

Java 1.5 and above

Example 1

Test it Now

Output:

Type safe view is: [A, B, C, D]

Example 2

Test it Now

Output:

Type safe view is: [10, 20, 30, 40]

Example 3

Test it Now

Output:

[apple, banana]
Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer element into collection with element type class java.lang.String
	at java.base/java.util.Collections$CheckedCollection.typeCheck(Collections.java:3038)
	at java.base/java.util.Collections$CheckedCollection.add(Collections.java:3081)
	at myPackage.CollectionCheckedCollectionExample3.main(CollectionCheckedCollectionExample3.java:12)






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA