How to Fix java.lang.ClassCastException in TreeSet?
TreeSet class in Java implements the Set interface that uses a tree for storing elements which contain unique objects stored in the ascending order. You may come across an exception called java.lang.ClassCastException while working with TreeSet objects. Basically, TreeSet elements are ordered using natural ordering or by using the Comparator defined in the constructor. If both don’t happen i.e natural ordering not occurring and also did not provide any comparator then java throws an exception which is java.lang.ClassCastException.
Example
Java
import java.util.TreeSet;
class Student {
int marks;
public Student( int marks) { this .marks = marks; }
public String toString()
{
return "Student marks = " + this .marks;
}
}
class GFG {
public static void main(String[] args)
{
TreeSet<Student> treeSet = new TreeSet<Student>();
treeSet.add( new Student( 1 ));
System.out.println(treeSet);
}
}
|
Output
Exception in thread “main” java.lang.ClassCastException: class Student cannot be cast to class java.lang.Comparable (Student 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:1291)
at java.base/java.util.TreeMap.put(TreeMap.java:536)
at java.base/java.util.TreeSet.add(TreeSet.java:255)
at GFG.main(File.java:31)
We can resolve this exception in two ways:
- By implementing the Comparable interface
- By defining custom Comparator class
Approach 1(Implementing Comparable Interface)
Java Comparable interface is implemented by a class by which used to compare and sort the objects according to the natural ordering. Natural ordering is possible using compareTo() function. String objects and wrapper class objects are sorted according to the built-in compareTo() function.
If compareTo function returns positive or negative or zero, then the current object is greater, lesser, and equal to the provided object respectively.
Example 1:
Java
import java.util.TreeSet;
class Student implements Comparable<Student> {
int id;
String name;
int marks;
public Student( int id, String name, int marks)
{
this .id = id;
this .name = name;
this .marks = marks;
}
public int compareTo(Student obj)
{
return this .marks - obj.marks;
}
public String toString()
{
return "Id: " + this .id + " Name: " + this .name
+ " Marks: " + this .marks;
}
}
class GFG {
public static void main(String[] args)
{
TreeSet<Student> treeSet = new TreeSet<Student>();
treeSet.add( new Student( 1 , "Suresh" , 87 ));
treeSet.add( new Student( 2 , "Ramesh" , 78 ));
treeSet.add( new Student( 3 , "Lokesh" , 95 ));
System.out.println(treeSet);
}
}
|
Output
[Id: 2 Name: Ramesh Marks: 78, Id: 1 Name: Suresh Marks: 87, Id: 3 Name: Lokesh Marks: 95]
Approach 2(Using Custom Comparator class)
A comparator is an interface that has to implemented by the class by which we can sort the objects of the user-defined class. It has 2 main methods that are used widely, compare(T o1, T o2) and equals(Object obj) which returns an int and boolean respectively. Let us implement the same example using a comparator.
Java
import java.util.*;
class Student {
int id;
String name;
int marks;
public Student( int id, String name, int marks)
{
this .id = id;
this .name = name;
this .marks = marks;
}
public int getMarks() { return this .marks; }
public String toString()
{
return "Id: " + this .id + " Name: " + this .name
+ " Marks: " + this .marks;
}
}
class StuComparator implements Comparator<Student> {
public int compare(Student obj1, Student obj2)
{
return obj1.getMarks() - obj2.getMarks();
}
}
class GFG {
public static void main(String[] args)
{
TreeSet<Student> treeSet
= new TreeSet<Student>( new StuComparator());
treeSet.add( new Student( 1 , "Suresh" , 87 ));
treeSet.add( new Student( 2 , "Ramesh" , 78 ));
treeSet.add( new Student( 3 , "Lokesh" , 95 ));
System.out.println(treeSet);
}
}
|
Output
[Id: 2 Name: Ramesh Marks: 78, Id: 1 Name: Suresh Marks: 87, Id: 3 Name: Lokesh Marks: 95]
Last Updated :
04 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...