Open In App

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

Last Updated : 01 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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 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();
    }
}
 
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);
    }
}


Output

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 interface
class 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 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();
    }
}
 
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);
    }
}


Output

The Treemap : {Chetan : 300=3, Akshay : 500=1, Bhanu : 600=2}

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads