Monday, 19 February 2018

Debugging collections with checkedCollection()

Download One Minute Java mobile app for latest updates

Overview
checkedCollection() is a static method in Collections class
Java Docs says checkedCollection() returns a dynamically typesafe view of the specified collection. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.
To understand what the previous statement mean, lets first have look at the problem which checkedCollection() helps us to solve

Problem Statement
While writing huge code it might happen we insert incorrect type of elemnt in parameterised collection. Later on somewhere down the code we try to perform some operation on the collection and at that point we get a ClassCastException.
In big code it really takes a lot of time to identify the exact point where incorrect element was inserted. This is because the classCastException indicates the line where the operation is failed and not the line where the element is inserted
Lets look at this problem with below example

Example

  public class CheckedCollectionExample
 {
    public static void main(String[] args)
    {
     1 List &ltString&gt cities = new ArrayList<>();
     2 Collections.addAll(cities, "Pune", "Nashik", "Dhule");

     3 List listInterface = cities;

     4 List codes = listInterface;
     5 codes.add(42);

     6 System.out.println(String.join(",", cities));

   }
  }


Error Message

  Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence
    at java.lang.String.join(Unknown Source)
    at CheckedCollectionExample.main(CheckedCollectionExample.java:6)


Error Message Analysis
The above error message points at the line of operation and not at the line of element insertion, this can be a real pain in big code

How checkedCollection() can help
Lets see below example

  public class CheckedCollectionExample
 {
    public static void main(String[] args)
    {
     1 List &ltString&gt cities = Collections.checkedList(new ArrayList<>(), String.class);
     2 Collections.addAll(cities, "Pune", "Nashik", "Dhule");

     3 List listInterface = cities;

     4 List codes = listInterface;
     5 codes.add(42);

     6 System.out.println(String.join(",", cities));

   }
  }


Error Message

  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.util.Collections$CheckedCollection.typeCheck(Unknown Source)
    at java.util.Collections$CheckedCollection.add(Unknown Source)
    at CheckedCollectionExample.main(CheckedCollectionExample.java:5)


Error Message Analysis
As you can see above error message clearly mention the cause as well as points to the corect line where incorrect element is inserted
So next time whenever you come across classCastException in your code you can utilise checkedCollection() method

No comments:

Post a Comment