Solved Error: cannot be cast to java.lang.Comparable

This article will look at how to solve the above-given Class Cast Exception. This ClassCast Exception occurs at runtime when we improperly typecast an object of a class to a related class of which it is not an instance.

Now, what does it mean when the exception says: cannot be cast to java.lang.Comparable. Let us first have a brief look at what is Comparable.

We use Comparable Interface mainly when we need to sort an Array of Custom Objects. In other words, it orders the objects of the user-defined class in a single sorting sequence. This interface is present in java.lang package and contains only one method named compareTo(Object), responsible for sorting these custom objects.

Now, let us look at different reasons we get the above exception in detail along with the way to resolve them.

Sorting Custom Objects of a Class

We can get this exception when we try to sort an Array of Objects without providing the implementation of sorting those objects. In such cases, we need to implement a Comparable interface. For example, consider this class:

If we have an array of Student Objects and suppose we need to sort those objects on the basis we cannot sort them simply using Arrays.sort() method of array class. This will result in an Exception. So for Example:

Calling the sort method in such a way will result in the exception:

This is because the sort method of Arrays Class provides sort implementation for only Primitive datatypes wrapped into Wrapper Class Objects. Hence, To Sort Custom Objects we need to provide the property which will sort the Array. As objects have more than one property or field.

Solution:

So, the solution will be to implement the Comparable interface and provide the sorting logic for each object by overriding its compareTo() method on basis of one field. We will have to provide two a single object as the argument and it will compare its field with the current instance. We have to implement the Interface in the same class of which the objects are made i.e. Student. So the Student Class now is:

Here, for simplicity, we sort the students according to their id. We can also sort them according to their name. After doing this we can call the sort method and sort the Student Array without getting an exception now and print the details.

Note: Using Comparable, the original class gets modified we cannot implement this in our Main Class, if we need to do it in a separate class we have to use the Comparator interface.

Using TreeSet or TreeMap or Any Ordered Collection to Store Custom Objects

This is another case where we might get this ClassCast Exception. Basically, the collection class or data structures such as Tree-Set and Tree-Map store ordered data. And, for general data types like Integer, Float, etc the classes have implemented the Comparable Interface to order them but not for Custom made objects. Suppose we have the same class Student (Not implementing Comparable) as above. Now instead of adding them in the array if we add them in Collections such as Tree Set or Tree-Map as shown below:

In this case, we get an exception when we add the first object to the Tree-Set as the student class defines no order on the objects. This will also happen if we add any object as a Key to a Tree-Map along with a value. The Exception we get here is different.

You can see the Exception is due to add() method of Tree-Set as the class Student has no functionality to compare objects it raises an exception.

Solution:

The solution for this will also be the same as above. We need to implement the Comparable Interface within the class Student or a Comparator in a different class (Not Main Class) and define an order on those objects by overriding their compare methods respectively. So, we implement the Comparable Interface here, and let’s define an order which will sort our objects according to Student’s name unlike the id’s what we did earlier. So the Student Class now is:

We sort the Students with respect to their names by overriding the compareTo() of their class. After this, we can execute the StudentMain Class without any exception and print the Student Details.

Output:

As you can see we print the student details sorted according to their names. We can also do this using a Tree-Map.

That’s all for the article, we discussed the reasons for the exception and saw the solution. Let us know about your queries in the comment section below.

Leave a Reply

Your email address will not be published.