困惑如何在另一个类中键入比较器

时间:2012-01-24 16:45:58

标签: java generics comparator

我正在尝试对两个LinkedHashMap的值进行排序。我可以编译它并运行代码就好了,但它告诉我在编译期间使用-Xlint选项,因为它是不安全的代码。它与类型铸造的东西有关,但我对如何做到这一点感到很困惑。我上了这堂课,我把它放在班上了:

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());

3 个答案:

答案 0 :(得分:4)

比较器是通用接口。这样做:

static class MyComparator implements Comparator<Map.Entry<String, Integer>> {
    public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2){
        ...
    }
}

并将您的列表定义为

List<Map.Entry<String, Integer>> myArrayList = new ArrayList<Map.Entry<String, Integer>>()

编译器会再次开心。

阅读the Generics Tutorial了解详情。或Angelika Langer's Generics FAQ

顺便说一句,除非您的比较器需要运行时参数或具有可变状态,否则应将其定义为常量,而不是为每次调用创建一个新实例

答案 1 :(得分:0)

您应该使用Comparator<T>界面而不是原始Comparator

阅读this article

答案 2 :(得分:0)

您可以采用以下类型安全的方式执行此操作:

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);

输出:

{four=4, one=1, five=5, three=3, two=2}
{one=1, two=2, three=3, four=4, five=5}