[問題]關於comparator& Map
Map<String , Integer> items
= new TreeMap<String,Integer>(new myComparator());
我自己建了個class myaComparator
準備排序TreeMap
可是在黃色的地方卻出錯了
public class myComparator implements Comparator{
public int compare(Object o1, Object o2) {
Map.Entry e1 = ( Map.Entry) o1;
---> Map.Entry e2 = ( Map.Entry) o2;
Integer d1 = (Integer) e1.getValue();
Integer d2 = (Integer) e2.getValue();
if(d1>=d2)
return 1;
else
return -1;
}
}
錯誤訊息如下:
Exception in thread "main" java.lang.ClassCastException: java.lang.String
cannot be cast to java.util.Map$Entry
at myComparator.compare(myComparator.java:7)
at java.util.TreeMap.getEntryUsingComparator(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.containsKey(Unknown Source)
at Fptree.processItems(Fptree.java:45)
at test.main(test.java:13)
請問是哪裡有問題呢?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.155.114
推
12/30 23:59, , 1F
12/30 23:59, 1F
→
01/18 15:55, , 2F
01/18 15:55, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):