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());
を私は上記の質問に意図したとおりそれはmap_freq_by_dateの私の定義を表示しませんでした。これはLinkedHashMapとして定義されています。 –
archcutbank