How to fix java.lang.ClassCastException while using the TreeMap in Java? - GeeksforGeeks

01 Oct, 2021
The java.lang.ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.
When we use custom class objects as keys in the TreeMap and neither implements the comparable interface nor comparator interface, then the java.lang.ClassCastException arises.
So there are two ways to fix java.lang.ClassCastException while using the TreeMap in Java:
Using Comparable
Using Comparator
Method 1: Using Comparable
We can fix java.lang.ClassCastException by the objects used as keys of the TreeMap implement the Comparable interface.
Pseudo Code:
// Custom class Student implements comparable interface

class Student implements Comparable<Student> {

  String name;
  Integer marks;

  public Student(String name, Integer marks) {
    this.name = name;
    this.marks = marks;
  }

  // Override toString method
  public String toString() {
    return this.name + " : " + this.marks;
  }

  public int getMarks() {
    return this.marks;
  }

  // Override compareTo method that sort treemap in the ascending order of the marks
  public int compareTo(Student stu) {
    return this.getMarks() - stu.getMarks();
  }
}
Implementation:
Java
// Java program to demonstrate how to fix// java.lang.ClassCastException while using the TreeMap import java.util.*; // Custom class Student implements comparable interfaceclass Student implements Comparable<Student> {     String name;    Integer marks;     public Student(String name, Integer marks)    {        this.name = name;        this.marks = marks;    }     // Override toString method    public String toString()    {        return this.name + " : " + this.marks;    }     public int getMarks() { return this.marks; }     // Override compareTo method that sort treemap in the    // ascending order of the marks    public int compareTo(Student stu)    {        return this.getMarks() - stu.getMarks();    }} public class GFG {    public static void main(String[] args)    {         // New TreeMap        TreeMap<Student, Integer> map = new TreeMap<>();         map.put(new Student("Akshay", 500), 1);        map.put(new Student("Bhanu", 600), 2);        map.put(new Student("Chetan", 300), 3);         System.out.println("The Treemap : " + map);    }}
The Treemap : {Chetan : 300=3, Akshay : 500=1, Bhanu : 600=2}
Method 2: Using Comparator
We can fix java.lang.ClassCastException by provide a custom comparator to the constructor at the creation time of the TreeMap.
Pseudo Code: 
// Custom comparator

class MyComparator implements Comparator<Student> {
  // Compare method that sort TreeMap in the ascending order of the marks
  public int compare(Student stu1, Student stu2) {
    return stu1.getMarks() - stu2.getMarks();
  }
}
Implementation: 
Java
// Java program to demonstrate how to fix// java.lang.ClassCastException while using the TreeMap import java.util.*; // Custom class Student implements comparable interfaceclass Student {     String name;    Integer marks;     public Student(String name, Integer marks)    {        this.name = name;        this.marks = marks;    }     // Override toString method    public String toString()    {        return this.name + " : " + this.marks;    }     public int getMarks() { return this.marks; }} // Custom comparatorclass MyComparator implements Comparator<Student> {       // Compare method that sort TreeMap in the ascending    // order of the marks    public int compare(Student stu1, Student stu2)    {        return stu1.getMarks() - stu2.getMarks();    }} public class GFG {    public static void main(String[] args)    {         // New TreeMap        TreeMap<Student, Integer> map            = new TreeMap<>(new MyComparator());         map.put(new Student("Akshay", 500), 1);        map.put(new Student("Bhanu", 600), 2);        map.put(new Student("Chetan", 300), 3);         System.out.println("The Treemap : " + map);    }}
The Treemap : {Chetan : 300=3, Akshay : 500=1, Bhanu : 600=2}
 
anikakapoor
Java-Exceptions
java-TreeMap
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java