In this post under Java, I will explain with example the purpose of Collections “checkedCollection” method.
From Java 5 onwards, we started using Generics to ensure compile time type safety.
As a result of which we were unable to add element of one data type to collections of elements of another data type.
At compile time itself compiler will highlight these kind of mistakes and help developers. This is good.
But they are situations where at runtime we may end up adding element of one data type to collections of elements of another data type.
For example lets say you have collection of integer type and we pass it to below method
public static void addition(Collection rawIntegerCollection) {
rawIntegerCollection.add(new String("hello"));
}
The above method takes a Collection of raw type and adds a new element of type String.
After the method execution, the String “hello” is added in a collection of integer type.
Compiler doesn’t highlight the problem here because according to compiler it is correct.
But at runtime since there is no check, the collection of type parameter integer will have an element of type String.
To avoid this kind of scenario Java Collections provide “checkedCollection” method.
This method takes a collection as a method parameter and returns another collection which does type checking at runtime. We can call this collection as “checked collection”.
This time when we call the above “addition” method again with this newly created “checked collection”. The String element is not added to the collection of integer and an exception is thrown.
Below is the complete main code for your reference.
Main class
1 package core.collection;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6
7 public class CheckedListDemo {
8 public static void main(String[] args) {
9 Collection<Integer> integerCollection1 = new ArrayList<>(0);
10 integerCollection1.add(1);
11
12 addition(integerCollection1);
13
14 System.out.println(integerCollection1);
15
16 Collection<Integer> integerCollection2 = new ArrayList<>(0);
17 integerCollection2.add(2);
18
19 Collection<Integer> checkedIntegerCollection = Collections.checkedCollection(integerCollection2, Integer.class);
20
21 addition(checkedIntegerCollection);
22
23 System.out.println(integerCollection2);
24 }
25
26 public static void addition(Collection rawIntegerCollection) {
27 rawIntegerCollection.add(new String("hello"));
28 }
29 }
In the above code, at line 9 I create a collection of element type Integer and populate it in subsequent lines.
At line 12, I call the static “addition” method which takes a raw collection and add an element of type String.
I call this method and pass the integer collection. At runtime the string is added in the collection of integers. This is wrong but since there is no type check at runtime this mistake happens.
This collection is printed to the console at line 14 successfully without any issues.
At line 16, I create another collection of element type Integer named “integerCollection2” and populate it with one element.
At line 19, I create checked version of “integerCollection2” with name “checkedIntegerCollection”.
At line 21, I pass “checkedIntegerCollection” to static method “addition”.
This “addition” method at runtime again attempt to add an element of type String to this checked collection but fails and throws an exception.
In this way we can use Collections’ “checkedCollection” method.
Below is the output
Output
[1, hello]
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:3355)
at java.base/java.util.Collections$CheckedCollection.add(Collections.java:3403)
at JavaConcepts.main/core.collection.CheckedListDemo.addition(CheckedListDemo.java:27)
at JavaConcepts.main/core.collection.CheckedListDemo.main(CheckedListDemo.java:21)