[英]Confused how to type comparator in another class
I am trying to sort the values of two LinkedHashMap. 我试图对两个LinkedHashMap的值进行排序。 I can compile it and run the code just fine, but it tells me to use -Xlint option during compiling because it is unsafe code.
我可以编译它并运行代码就好了,但它告诉我在编译期间使用-Xlint选项,因为它是不安全的代码。 It has something to do with type casting stuff, but I am royally confused on how to do it.
它与类型铸造的东西有关,但我对如何做到这一点感到很困惑。 I got this class which I put inbedded in my class:
我上了这堂课,我上课了:
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;
}
}
I tried to call it in one of my functions with: 我尝试在我的一个函数中调用它:
ArrayList myArrayList=new ArrayList(this.map_freq_by_date.entrySet());
Collections.sort(myArrayList, new MyComparator());
Iterator itr=myArrayList.iterator();
Note: this.map_freq_by_date is defined as follows: 注意:this.map_freq_by_date定义如下:
Map<String,Integer> map_freq_by_date = new LinkedHashMap<String,Integer>();
The error I get with -Xlint option: 我用-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());
Help with how to fix these would be appreciated. 如何解决这些问题的帮助将不胜感激。 I looked online and tried all kinds of things shown, but I can't seem to get it right.
我在网上看了一下并尝试了各种各样的东西,但我似乎无法做到正确。
Note: if I put ArrayList<Object> myArrayList = new ArrayList<Object>
... the error changes to: 注意:如果我放入
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());
Comparator is a Generic Interface. 比较器是通用接口。 Do it like this:
像这样做:
static class MyComparator implements Comparator<Map.Entry<String, Integer>> {
public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2){
...
}
}
and define your list as 并将您的列表定义为
List<Map.Entry<String, Integer>> myArrayList = new ArrayList<Map.Entry<String, Integer>>()
And the compiler will be happy again. 编译器会再次开心。
Read the Generics Tutorial for more info. 阅读泛型教程以获取更多信息。 Or Angelika Langer's Generics FAQ .
或Angelika Langer的泛型常见问题解答 。
Btw, unless your Comparator needs runtime parameters or has mutable state, you should define it as a Constant instead of creating a New instance for every call 顺便说一句,除非您的Comparator需要运行时参数或具有可变状态,否则您应该将其定义为Constant而不是为每个调用创建一个New实例
You should use Comparator<T>
interface not a raw Comparator
. 您应该使用
Comparator<T>
接口而不是原始Comparator
。
Read this article . 阅读这篇文章 。
You can do it in a type safe way as follows: 您可以使用以下类型安全的方式执行此操作:
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("four", 4);
map.put("one", 1);
map.put("five", 5);
map.put("three", 3);
map.put("two", 2);
System.out.println(map);
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
map.clear();
for(Map.Entry<String, Integer> e : entryList) {
map.put(e.getKey(), e.getValue());
}
System.out.println(map);
Output: 输出:
{four=4, one=1, five=5, three=3, two=2} {one=1, two=2, three=3, four=4, five=5}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.