How to Fix java.lang.ClassCastException in TreeSet?


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. To fix this ClassCastException in TreeSet we can use either the Comparator Interface or the Comparable Interface. Let's discuss them in detail.

The general syntax for TreeSet is as follows:

Syntax

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

Fixing java.lang.ClassCastException in TreeSet

In this section, we are going to explain how to use the Comparator and Comparable Interfaces to fix the ClassCastException in TreeSet. Let's start this discussion with an example program that will show us the ClassCastException.

Example 1

In the following example, we will try to add custom class objects to TreeSet without a Comparator and Comparable 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)

How to Fix java.lang.ClassCastException using Comparator

First, let's introduce the Comparator Interface.

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
   }
}    

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) { // performing comparison
         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

How to Fix java.lang.ClassCastException using Comparable Interface

Let's begin the discussion by introducing the Comparable Interface.

Comparable Interface

This interface is useful when we want to sort custom objects by their natural ordering. For example, It sorts strings in dictionary order and numerics in numerical order. This interface is available in 'java.lang' package.

Syntax

class nameOfclass implements Comparable<nameofclass>

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

compareTo()

The Comparable interface defines only a single method named 'CompareTo' that can be overridden in order to sort the collection of objects. It gives the power to compare the objects of a class to itself. It returns 0 when 'this' object is equal to the passed object, a positive value if 'this' object is greater otherwise a negative value.

Syntax

compareTo(nameOfclass nameOfobject);

Example 3

The following example demonstrates the use of a Comparable in fixing ClassCastException.

Approach

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

  • Create a class 'ShopBag' that implements a Comparable Interface. 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.

  • Define 'compareTo' method along with an object of class 'ShopBag' as a parameter to compare 'this' object with the newly created object.

  • Now, in the main() method, declare an object named 'trSet' of class 'ShopBag' of collection type TreeSet and using an in-built method named 'add()' store the details of object to the collection.

  • Use the for-each loop to print the elements of the collection.

import java.util.*;
public class ShopBag implements Comparable<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;
   }
   // overriding method
   public int compareTo(ShopBag comp) {
      if(this.price > comp.price) { // performing comparison 
         return 1;
      } else {
         return -1;
      }
   }
   public String toString() {
      return "Item: " + this.item + ", Price: " + this.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));
      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 and Comparable Interface that can be helpful in fixing the ClassCastException. Then, we discussed three example programs that show ClassCastException and how to fix this exception.

Updated on: 20-Jul-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements