The Collections.checkedCollection() method returns a runtime type-safe view of the specified collection. It ensures that only elements of the specified type can be added to the collection. If an element of an invalid type is inserted, a ClassCastException is thrown.
- Prevents insertion of elements of the wrong type.
- Throws ClassCastException if an invalid type is added.
- Useful when working with legacy or raw collections.
import java.util.*;
public class CheckedCollectionDemo {
public static void main(String[] args) {
// Creating a collection
Collection<String> names = new ArrayList<>();
// Creating a type-safe view
Collection<String> checkedNames =
Collections.checkedCollection(names, String.class);
// Adding elements
checkedNames.add("Alice");
checkedNames.add("Bob");
checkedNames.add("Charlie");
// Displaying the collection
System.out.println("Names: " + checkedNames);
}
}
Output
Names: [Alice, Bob, Charlie]
Explanation: This example creates a type-safe view of an ArrayList using Collections.checkedCollection(). The returned collection allows only String elements to be added, helping prevent accidental insertion of objects of the wrong type at runtime.
Syntax:
public static <E> Collection<E> checkedCollection(
Collection<E> c, Class<E> type)
Parameters: This method takes two parameters
- c –> The collection for which a runtime type-safe view is to be created.
- type –> The
Classobject representing the type of elements allowed in the collection.
Return Type: Returns a runtime type-safe view of the specified collection that allows only elements of the specified type.
Example: Using checkedCollection() with String
import java.util.*;
public class CheckedCollectionExample1 {
public static void main(String[] args) {
// Creating an ArrayList of String
List<String> list = new ArrayList<>();
// Adding elements
list.add("Java");
list.add("Python");
list.add("C++");
list.add("JavaScript");
System.out.println("Original List: " + list);
// Creating a type-safe view of the collection
Collection<String> checkedList =
Collections.checkedCollection(list, String.class);
System.out.println("Type-safe Collection: " + checkedList);
}
}
Output
Original List: [Java, Python, C++, JavaScript] Type-safe Collection: [Java, Python, C++, JavaScript]
Explanation: This example creates a type-safe view of a String collection using Collections.checkedCollection(). It ensures that only String objects can be added to the collection at runtime.
Example: Using checkedCollection() with Integer
import java.util.*;
public class CheckedCollectionExample2 {
public static void main(String[] args) {
// Creating an ArrayList of Integer
List<Integer> list = new ArrayList<>();
// Adding elements
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println("Original List: " + list);
// Creating a type-safe view of the collection
Collection<Integer> checkedList =
Collections.checkedCollection(list, Integer.class);
System.out.println("Type-safe Collection: " + checkedList);
}
}
Output
Original List: [10, 20, 30, 40] Type-safe Collection: [10, 20, 30, 40]
Explanation: This example creates a type-safe view of an Integer collection using Collections.checkedCollection(). The returned collection allows only Integer elements, helping prevent runtime type errors.
Advantages
- Provides runtime type safety for collections.
- Prevents insertion of objects of the wrong type.
- Helps detect programming errors early.
- Useful when working with legacy code that uses raw collections.
- Improves the reliability and maintainability of collection-based code.