Java kann bei Verwendung von TreeMap nicht in Comparable umgewandelt werden [duplizieren]
Mögliche Duplikate:
Java: SortedMap, TreeMap, Vergleichbar? Wie benutzt man?
Ich benutze JavaJungI Grafikpaket und Netbeans 7. Ich erhalte die folgende Fehlermeldung von 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)
Hier ist der Code für den Fehler:
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**
}
Klasse MyVertex ist eine Klasse, die ich für Diagramme erstellt habe. Das Folgende ist der Code für 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
}
}
Wie kann ich MyVertex-Typen in Comparables umwandeln?Warum ist das notwendig? (Ich sehe nicht sofort den Grund)