2
我需要梳理的地图使用地图和lambda表达式作为参数排序的方法值,而地图有结构,如:排序的Java地图由值列表
Map<T,List<T>>=
Groovy = [Z, Y, X, D]
Java = [V, B, C, D, A, Z]
C++ = [G, J, H]
C# = [P, S, Q, V, D]
Scala = [A, D]
我的排序方法:
sorted(Map<T,List<T>> map,Comparator<Map<T,List<T>>> comp)
,然后在另一个负责从文件读取数据并将其放入地图的函数中实现它。这是我的排序方法:
public Map<T,List<T>> sorted(Map<T,List<T>> map, Comparator<Map<T,List<T>>> comp){
List list = new LinkedList(map.entrySet());
Collections.sort(list, comp);
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
return sortedHashMap;
}
这就是我如何在另一种方法使用它:
Comparator<Map<T,List<T>>> comp = new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}};
iniMap=sorted(iniMap,comp);
当我运行我的程序,我得到以下错误:
java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.Comparable
任何帮助将不胜感激,我有点卡住了。