Java "no se puede convertir a Comparable" cuando se utiliza TreeMap [duplicado]
Posible duplicado:
Java: SortedMap, TreeMap, Comparable? ¿Cómo utilizar?
Estoy usando el javaJungi Graph package y Netbeans 7. Recibo el siguiente error de Java:
Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
Aquí está el código asociado con el error:
SortedMap<MyVertex, Double> vMap = new TreeMap<MyVertex, Double>();
double curRank = 0;
for(MyVertex v: g.getVertices()) //g is a SparseGraph<MyVertex, MyEdge>
{
curRank = vertexRank.getVertexScore(v);
vMap.put(v, curRank); //**Here is my Error**
}
La clase MyVertex es una clase que hice para los gráficos. El siguiente es el código para MyVertex
public class MyVertex
{
int vID; //id for this vertex
double centrality; //centrality measure for this vertex
int degree; //the degree of this vertex
public MyVertex(int id)
{
this.vID = id;
this.centrality=0;
this.degree=0;
}
public double getCentrality()
{
return this.centrality;
}
public void setCentrality(double centrality)
{
this.centrality = centrality;
}
public int getDegree()
{
return this.degree;
}
public void setDegree(int deg)
{
this.degree = deg;
}
public void incrementDegree()
{
this.degree++;
}
public void decrementDegree()
{
this.degree--;
}
@Override
public String toString()
{
return "v"+vID;
}
int compareTo(MyVertex v)
{
return (this.degree < v.degree) ? 1 : 0; //this will do descendingly
}
}
¿Cómo hago para que los tipos MyVertex se conviertan en comparables?¿Por qué es esto necesario? (No veo de inmediato el motivo)