The checkedCollection() method of java.util.Collections class is used to return a dynamically typesafe view of the specified collection
The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object’s equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list.
The returned collection will be serializable if the specified collection is serializable.
Since null is considered to be a value of any reference type, the returned collection permits insertion of null elements whenever the backing collection does.
public static Collection checkedCollection(Collection c, Class type)
Parameters: This method takes the following arguments as a parameter:
- c – the collection for which a dynamically typesafe view is to be returned
- type – the type of element that c is permitted to hold
Return Value: This method returns a dynamically typesafe view of the specified collection
Below are the examples to illustrate the checkedCollection() method
List: [CSS, PHP, HTML, TajMahal] Typesafe view of List: [CSS, PHP, HTML, TajMahal]
List: [20, 30, 40, 50] Typesafe view of List: [20, 30, 40, 50]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.