Vote count:
0
I have the following two classes which define objects I would like to put into a TreeMap:
class GeneKey {
String PN;
int PW;
// Generator makes unique TreeMap key.
GeneKey(String a, int b){
this.PN = a;
this.PW = b;
}
}
Then a second object:
class GeneValue {
String info;
String date;
// Generator makes TreeMap value
GeneValue(String a, String b){
this.info = a;
this.date = b;
}
}
I would like to then make a TreeMap:
import java.util.TreeMap;
// In main ...
TreeMap<GeneKey, GeneValue> samples = new TreeMap<GeneKey, GeneValue>();
String a = "test";
int b = 100;
String c = "test again";
String d = "test yet again";
// Try to put these objects into the tree map.
samples.put(new GeneKey(a, b) ,new GeneValue(c,d))
But I get the following error:
Exception in thread "main" java.lang.ClassCastException: GeneKey cannot be cast to java.lang.Comparable
I would like to know why I am not able to establish a TreeMap with key:value of GeneKey:GeneValue even though I specified those objects when initializing my TreeMap. How to I initialize the map in order to .put() these two objects.
Thank you
asked 1 min ago
Error when putting
Aucun commentaire:
Enregistrer un commentaire