Java: Using TreeSet with a Class type -
i having problems using treeset sort hashmap. following code have:
private static hashmap<oddmove, integer> sorthashmap( hashmap<oddmove, integer> hm) { map<oddmove, integer> tempmap = new hashmap<oddmove, integer>(); (oddmove wsstate : hm.keyset()) { tempmap.put(wsstate, hm.get(wsstate)); } list<oddmove> mapkeys = new arraylist<oddmove>(tempmap.keyset()); list<integer> mapvalues = new arraylist<integer>(tempmap.values()); hashmap<oddmove, integer> sortedmap = new linkedhashmap<oddmove, integer>(); treeset<oddmove> sortedset = new treeset<oddmove>(mapkeys); object[] sortedarray = sortedset.toarray(); int size = sortedarray.length; (int = 0; < size; i++) { sortedmap.put(mapkeys.get(mapvalues.indexof(sortedarray[i])), (integer) sortedarray[i]); } return sortedmap; }
following error getting:
aborting: exception in odd.ueplayer3.chosemove() java.lang.classcastexception: odd.oddmove cannot cast java.lang.comparable @ java.util.treemap.compare(treemap.java:1188) @ java.util.treemap.put(treemap.java:531) @ java.util.treeset.add(treeset.java:255) @ java.util.abstractcollection.addall(abstractcollection.java:334) @ java.util.treeset.addall(treeset.java:312) @ java.util.treeset.<init>(treeset.java:160) @ odd.ueplayer3.sorthashmap(ueplayer3.java:196) @ odd.ueplayer3.choosemove(ueplayer3.java:101) @ boardgame.client.playmove(client.java:109) @ boardgame.client.processmessage(client.java:86) @ boardgame.client.clientloop(client.java:177) @ boardgame.client.run(client.java:73) @ java.lang.thread.run(thread.java:722)
would appreciate help
i'm guessing oddmove custom class you've written. if that's case, you'll need update in order implement comparable
interface.
Comments
Post a Comment