How to Fix java.lang.ClassCastException in TreeSet By Using Custom Comparator in Java?


The TreeSet is a generic class of Java Collection Framework that implements the SortedSet Interface. It stores the elements of the set in a tree structure. Furthermore, all the elements are stored in a sorted manner and they must be mutually comparable if we are attempting to add custom class objects otherwise we will encounter a java.lang.ClassCastException. Here, custom class objects mean user-defined objects that are created with the help of a constructor. One way to fix this ClassCastException is by using a custom comparator. Let's discuss it in detail.

The general syntax for TreeSet is as follows:

Syntax

TreeSet<TypeOfSet> nameOfSet = new TreeSet<>(); 

Fixing java.lang.ClassCastException in TreeSet using Comparator

In this section, we are going to explain how to use the custom comparator to fix the ClassCastException in TreeSet. Let's start this discussion with Comparator.

Comparator

As the name suggests it is used to compare something. In Java, the Comparator is an interface that is used to sort custom objects. We can write our own logic inside its inbuilt method named 'compare()' to sort the specified objects. This method takes two objects as arguments and then returns an integer value. By this integer value, Comparator decides which object is greater.

Syntax

class nameOfComparator implements Comparator< TypeOfComparator >() {
	compare( type object1, type object2 ) {
		// logic for comparison
	}
}  

Here, class is the keyword that creates a Class and implements is the keyword that enables the use of features provided by an Interface.

Example 1

In the following example, we will try to add custom class objects to TreeSet without a Comparator to show that the Java compiler throws java.lang.ClassCastException.

import java.util.*;
public class ShopBag { 
   String item;
   int price;
   ShopBag(String item, int price) { // constructor
	// this keyword shows these variables belong to constructor
      this.item = item; 
      this.price = price;
   }
   public static void main(String[] args) {
      // Declaring collection TreeSet
      TreeSet<ShopBag> trSet = new TreeSet<ShopBag>();
      // Adding object to the collection
      trSet.add(new ShopBag("Rice", 59));
      trSet.add(new ShopBag("Milk", 60));
      // to print the objects
      for (ShopBag print : trSet) {
         System.out.println("Item: " + print.item + ", " + "Price: " + print.price);
      }
   }
}

Output

Exception in thread "main" java.lang.ClassCastException: class ShopBag cannot be cast to class java.lang.Comparable (ShopBag is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
	at java.base/java.util.TreeMap.compare(TreeMap.java:1569)
	at java.base/java.util.TreeMap.addEntryToEmptyMap(TreeMap.java:776)
	at java.base/java.util.TreeMap.put(TreeMap.java:785)
	at java.base/java.util.TreeMap.put(TreeMap.java:534)
	at java.base/java.util.TreeSet.add(TreeSet.java:255)
	at ShopBag.main(ShopBag.java:14)

Example 2

The following example illustrates the use of a Comparator in fixing ClassCastException.

Approach

  • First, import the 'java.util' package so that we can work with TreeSet.

  • Create a class 'ShopBag'. Inside it, declare two variables and define a constructor of this class along with two parameters 'item' and 'price' of type string and integer respectively.

  • Then, define another class 'Comp' that implements Comparator Interface and inside it, use the 'compare()' method to sort the TreeSet in ascending order.

  • Inside the 'main()' method, create a TreeSet collection by passing the instance of 'Comp' class so that it can get sorted.

  • In the end, store a few elements into the TreeSet collection using 'add()' method and then use the for-each loop to print the elements of the collection.

import java.util.*;
class ShopBag { 
   String item;
   int price;
   ShopBag(String item, int price) { // constructor
	// this keyword shows these variables belong to constructor
      this.item = item; 
      this.price = price;
   }
}
// use of comparator interface
class Comp implements Comparator<ShopBag> {
   // logic to sort
   public int compare(ShopBag i, ShopBag j) {
      if(i.price > j.price) {
         return 1;
      } else {
         return -1;
      }
   }
}
public class Example2 {
   public static void main(String[] args) {
      // Declaring collection TreeSet 
      TreeSet<ShopBag> trSet = new TreeSet<ShopBag>(new Comp());
      // Adding object to the collection
      trSet.add(new ShopBag("Rice", 59));
      trSet.add(new ShopBag("Milk", 60));
      trSet.add(new ShopBag("Bread", 45));
      trSet.add(new ShopBag("Peanut", 230));
      trSet.add(new ShopBag("Butter", 55));
	   System.out.println("Objects of the collection: ");
      // to print the objects
      for (ShopBag print : trSet) {
         System.out.println("Item: " + print.item + ", " + "Price: " + print.price);
      }
   }
}

Output

Objects of the collection: 
Item: Bread, Price: 45
Item: Butter, Price: 55
Item: Rice, Price: 59
Item: Milk, Price: 60
Item: Peanut, Price: 230

Conclusion

We started this article by defining TreeSet and introducing the ClassCastException of TreeSet. In the next section, we introduced the Comparator Interface that can be helpful in fixing the ClassCastException. Then, we discussed two example programs that show ClassCastException and how to fix this exception.

Updated on: 20-Jul-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements