How to fix java.lang.ClassCastException while using the TreeMap in Java? Last Updated : 01 Oct, 2021 Comments Improve Suggest changes Like Article Like 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: Using ComparableUsing 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 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); } } OutputThe 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); } } OutputThe Treemap : {Chetan : 300=3, Akshay : 500=1, Bhanu : 600=2} Comment More infoAdvertise with us Next Article How to fix java.lang.ClassCastException while using the TreeMap in Java? S sachinchhipa44 Follow Improve Article Tags : Java Java Programs Java-Exceptions java-TreeMap Practice Tags : Java Similar Reads How to Update the Value for an Existing Key in a TreeMap Using put()? TreeMap is a part of Java Collection that can in-built class of java.util package. In Java, TreeMap is a pre-defined class that implements the NavigableMap interface and extends the AbstractMap class. It represents the Key-Value pairs. It is part of the Java Collection Framework.It is a sorted map b 2 min read How to Get TreeMap Key or Value using Index in Java? The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. The TreeMap class is a Red-Black tree implementation of the Map interface and thus does not expose any methods using whic 5 min read How to Create TreeMap Objects using Comparable Interface in Java? In Java, the TreeMap class is an implementation of the SortedMap interface that stores key-value pairs in a sorted order based on the natural ordering of the keys. By default, the keys are sorted in ascending order. If you want to sort the keys based on a custom ordering criteria, you can use the Co 6 min read How to Add Custom Class Objects to the TreeSet in Java? TreeSet is an implementation of the SortedSet interface in java that uses a red-black tree for storage. By default, It maintains an ascending order. It contains unique elements only. It doesn't allow null elements. Access and retrieval times are quite fast. To add the user-defined object into TreeSe 3 min read How to Copy Key-Value Pairs from One TreeMap to Another in Java? In Java, a TreeMap is a Map implementation that stores key-value pairs in a red-black tree structure. It allows insertions and deletions of key-value pairs due to its tree implementation. These operations take O(log n) time on average. In this article, we will be learning how to copy key-value pairs 2 min read How to Serialize and Deserialize a TreeMap in Java? In Java, serialization is implemented by using the Serializable Interface. The use of the TreeMap class is a simple way to serialize and deserialize the objects. It is a component of the Java Collections framework. For writing and reading objects, we will be using the ObjectOutputStream and ObjectIn 2 min read How to Convert TreeMap to an ArrayList in Java? TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot 4 min read Java Program to Check if the TreeMap is Empty The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Approaches: Using the isEmpty() methodU 3 min read How to Compare Two TreeMap Objects in Java? TreeMap class in java provides a way of storing key-value pairs in sorted order. The below example shows how to compare two TreeMap objects using the equals() method. It compares two TreeMap objects and returns true if both of the maps have the same mappings else returns false. Syntax: boolean equal 1 min read How to Sort a TreeMap By Value in Java? In Java Language, a TreeMap always stores key-value pairs which are in sorted order on the basis of the key. TreeMap implements the NavigableMap interface and extends AbstractMap class. TreeMap contains unique keys. Sorting TreeMap by value in Java The elements in TreeMap are sorted on the basis of 3 min read Like