2012-01-24 10 views
3

2つのLinkedHashMapの値をソートしようとしています。私はそれをコンパイルしてコードを正常に実行できますが、安全でないコードなのでコンパイル時に-Xlintオプションを使用するように指示します。それはタイプキャストのものとは関係がありますが、私はそれをどうやって行うのかについて王立に混乱しています。別のクラスにコンパレータを入力する方法が混乱しています

ArrayList myArrayList=new ArrayList(this.map_freq_by_date.entrySet()); 
Collections.sort(myArrayList, new MyComparator()); 
Iterator itr=myArrayList.iterator(); 

注:私はと私の機能の一つで、それを呼び出そうとしました

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

    } 

:私は、私は私のクラスでinbedded入れ、このクラスを持って次のように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()); 
+0

を私は上記の質問に意図したとおりそれはmap_freq_by_dateの私の定義を表示しませんでした。これはLinkedHashMap として定義されています。 – archcutbank

答えて

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

あなたのコンパレータは、実行時パラメータを必要とするか、変更可能な状態を持っていない限り、ところで、あなたの代わりにあなたがComparator<T>インタフェースではない生のComparatorを使用する必要があり、すべてのコール

+0

私はちょうどあなたが整数ではなく、マップの項目を比較したいと分かりました。その場合、 'Integer'のすべての出現を' Map.Entry 'に置き換えてください。 –

+0

ありがとう!それは警告を取り除いた。 -Xlintオプションを指定しないと、それ以上の警告はありません。あなたが知っている場合、ArrayList とArrayList >の違いは何ですか?また、ArrayListをListに変更する必要があるのはなぜですか? – archcutbank

+0

@ user372429 ArrayListには型変数が1つしかないので、 'ArrayList 'はコンパイルされません。変数型をListに変更する必要はありませんが、インプリメンテーション型ではなく、インタフェースに対してプログラムするのは良いスタイルと考えられます。 [Effective Java](http://java.sun.com/docs/books/effective/)、Item 52:[オブジェクトのインタフェース参照](http://my.safaribooksonline。com/book/programming/java/9780137150021/general-programming/ch08lev1sec8) –

0

の新しいインスタンスを作成するための定数として定義する必要があります。

this articleを読んでください。

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} 
関連する問題