Folgender Sachverhalt. Ich habe:
- eine Klasse Node, die eine Informationseinheit darstellt u.a. mit einer Eigenschaft "activation".
- eine TreeMap genannt stringNodeMapping, die Strings auf Nodes abbildet:
Durch die TreeMap kann ich mir so zu einem String die passende Node raussuchen. Jetzt möchte ich aber, dass die Elemente in der TreeMap immer nach "activation" sortiert sind. Leider klappt das nicht. Bei jeder möglichen Variante zeigt mir Eclipse unterschiedliche Warnings (z.B. can not cast x to y,...). Das hier ist z.B. ein Versuch:
Ich bin mir sicher, ich hab irgendwas mit den Generics falsch gemacht. Kann mir jemand sagen, wo der Fehler liegt?
- eine Klasse Node, die eine Informationseinheit darstellt u.a. mit einer Eigenschaft "activation".
- eine TreeMap genannt stringNodeMapping, die Strings auf Nodes abbildet:
Durch die TreeMap kann ich mir so zu einem String die passende Node raussuchen. Jetzt möchte ich aber, dass die Elemente in der TreeMap immer nach "activation" sortiert sind. Leider klappt das nicht. Bei jeder möglichen Variante zeigt mir Eclipse unterschiedliche Warnings (z.B. can not cast x to y,...). Das hier ist z.B. ein Versuch:
Code:
public class Node implements Comparable<Node>{
private float activation;
...
public int compareTo(Node otherNode) {
// -1: first node get's higher priority, 0: equal, 1: second node get's higher priority
int result = 0;
if (activation > otherNode.getActivation()) {
result = -1;
} else if (otherNode.getActivation() < activation) {
result = 1;
}
return result;
}
}
Code:
public class NodeComparator implements Comparator<Node>{
public int compare(Node node1, Node node2) {
// -1: first node get's higher priority, 0: equal, 1: second node get's higher priority
int result = 0;
if (node1.getActivation() > node2.getActivation()) {
result = -1;
} else if (node1.getActivation() < node2.getActivation()) {
result = 1;
}
return result;
}
}
Code:
public class MyApplication{
...
private TreeMap<String,Node> stringNodeMapping;
NodeComparator nodeComparator = new NodeComparator();
stringNodeMapping = new TreeMap<String,Node>(nodeComparator);
}
Ich bin mir sicher, ich hab irgendwas mit den Generics falsch gemacht. Kann mir jemand sagen, wo der Fehler liegt?