Reputation: 55
I'm trying create something like this tutorial on creating a graph and I'm coming across an error: Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable
.
Car class
Integer id;
String model;
String brand;
float price;
Cars class (both have been instantiated already):
TreeMap<Integer, Car> m;
TreeMap<Car, LinkedList<Car>> adjList;
Main:
for(Map.Entry<Integer, Car> entry : a.m.entrySet()){
Car val = entry.getValue();
LinkedList<Car> list = new LinkedList<Car>();
a.adjList.put(val, list);
}
This creates an adjList
for every Car object in m
but the problem is the error. I've tried making sure the entered variables are of the same type by using instanceof
and it returns true. So i'm not sure where the error comes from.
Upvotes: 1
Views: 77
Reputation: 5290
TreeMap is a SortedMap implementation and it requires its keys to implement the Comparable interface in order to sort the keys.
So I assume Car does not implement Comparable. If you add this implementation, you'll be fine.
Upvotes: 2