我想对两个LinkedHashMap的值进行排序。我可以编译它并运行代码,但它告诉我在编译期间使用-Xlint选项,因为它是不安全的代码。它与类型转换的东西有关,但我对如何去做这件事感到非常困惑。我得到这个类,我在班级把inbedded:混淆了如何在另一个类中输入比较器
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2){
int result=0;
Map.Entry e1 = (Map.Entry)obj1 ;
Map.Entry e2 = (Map.Entry)obj2 ;//Sort based on values.
Integer value1 = (Integer)e1.getValue();
Integer value2 = (Integer)e2.getValue();
if(value1.compareTo(value2)==0){
String word1=(String)e1.getKey();
String word2=(String)e2.getKey();
//Sort String in an alphabetical order
result=word1.compareToIgnoreCase(word2);
} else {
//Sort values in a descending order
result=value2.compareTo(value1);
}
return result;
}
}
我试图调用它与我的功能之一:
ArrayList myArrayList=new ArrayList(this.map_freq_by_date.entrySet());
Collections.sort(myArrayList, new MyComparator());
Iterator itr=myArrayList.iterator();
注:this.map_freq_by_date定义如下:
Map<String,Integer> map_freq_by_date = new LinkedHashMap<String,Integer>();
我-Xlint选项得到的错误:
unchecked call to ArrayList(java.util.Collection<? extends E>) as a member of the raw type java.util.ArrayList
ArrayList myArrayList=new ArrayList(this.map_freq_by_date.entrySet());
unchecked conversion
found LogGrep.MyComparator
required: java.util.Comparator(? super T>
Collections.sort(myArrayList, new MyComparator());
unchecked method invocation: <T>sort(java.util.List<T>,java.util.Comparator<? super T> in java.util.Collections is applied to (java.util.ArrayList,LogGrep.MyComparator)
Collections.sort(myArrayList, new MyComparator());
帮助如何解决这些将不胜感激。我在网上查看并尝试了各种所显示的内容,但我似乎无法正确理解。
注:如果我把ArrayList<Object> myArrayList = new ArrayList<Object>
...错误更改:
unchecked method invocation <T>sort(java.util.List<T>,java.util.Comparator<> super T?) in java.util.Collections is applied ot (java.util.ArraList<java.lang.Object>,LogGrep.MyComparator)
Collections.sort(myArrayList, new MyComparator());
它没有列出我map_freq_by_date的定义,我打算在这个问题上面。它被定义为一个LinkedHashMap。 –
archcutbank