1

HashMapsの値の並べ替えに問題がありますJava。私のコードは次のとおりです。

 @SuppressWarnings("unchecked")
          Map<String, Integer> scores = ((HashMap<String, Integer>) prefs.get());
          Map<String, Integer> sortedscores = sortByValues(scores);
          printMap(scores);
          System.out.println("==============");
          printMap(sortedscores);

prefs.get() は、Map<String, ?>私が変換する a を返します<String, Integer >

ソート機能:

public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
    Comparator<K> valueComparator =  new Comparator<K>() {
        public int compare(K k1, K k2) {
            int compare = map.get(k2).compareTo(map.get(k1));
            if (compare == 0) return 1;
            else return compare;
        }
    };
    Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return new LinkedHashMap<K,V>(sortedByValues);
}
public static void printMap(Map<String, Integer> unsortMap){
    for (Map.Entry entry : unsortMap.entrySet()) {
        System.out.println("Key : " + entry.getKey() 
                               + " Value : " + entry.getValue());
    }
}

出力は次のとおりです。

Key : John Doe Value : 1000
Key : balazs Value : 975
Key : Balazs Value : 900
Key : aladar Value : 975
Key : balazs2 Value : 975
Key : score Value : 1000
Key : house Value : 1037
==============
Key : balazs Value : 975
Key : aladar Value : 975
Key : balazs2 Value : 975
Key : Balazs Value : 900
Key : house Value : 1037
Key : John Doe Value : 1000
Key : score Value : 1000

最初のものはソートされていないもので、2 番目のものはソートされています。私の問題は、2 番目の出力が DESC 順 (値順) でないことです。

編集: 自分で hasmap を作成すると、正常に動作します:

Map<String, Integer> unsortMap = new HashMap<String, Integer>();
        unsortMap.put("asd", 1);
        unsortMap.put("asd2r1", 5);
        unsortMap.put("house", 7);
        unsortMap.put("3", 124);
        unsortMap.put("7", 4);
        unsortMap.put("5", 6);
        unsortMap.put("6", 2);
        unsortMap.put("8", 0);

しかし、これを試してみるとMap<String, Integer> scores = ((HashMap<String, Integer>) prefs.get());、奇妙な順序になります。

4

4 に答える 4

2

あなたのコンパレータは、仕様に準拠しているようには見えません:

        int compare = map.get(k2).compareTo(map.get(k1));
        if (compare == 0) return 1;
        else return compare;

2 つのエントリが等しいのに 1 を返すのはなぜですか?

于 2013-03-27T13:50:58.620 に答える
1

基本的にこれを書き直す必要があります:

public int compare(K k1, K k2) {
    int compare = map.get(k2).compareTo(map.get(k1));
    if (compare == 0) return 1;
    else return compare;
}

に:

public int compare(K k1, K k2) {
    return map.get(k2).compareTo(map.get(k1));
}

2 つの値が等しい場合、実際には一方が他方よりも大きいということになりますが、これはあまり意味がありません。キーが比較可能な場合は、自然な比較を使用します。

于 2013-03-27T13:52:09.120 に答える
0

問題はリターン文にあると思います:

Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return new LinkedHashMap<K,V>(sortedByValues);

並べ替えられたマップがあり、すべてのペアを新しいマップに配置すると、再び並べ替えが解除されます。これを試してください:

Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
于 2013-03-27T14:14:57.360 に答える
0

解決策が見つかりました:問題は、整数ではなく文字列を比較していたことです。私が試した変換

Map<String, Integer> scores = ((HashMap<String, Integer>) prefs.get());

整数に変換しませんでした。だから私はそれを正しく行うサイクルを使用しました:

   for (@SuppressWarnings("rawtypes") Map.Entry entry : scores.entrySet()) {
       scoresInt.put(entry.getKey().toString(), Integer.parseInt(entry.getValue().toString()));
   }

hasmap がソートに変換されると、魅力のように機能しました。

于 2013-03-27T15:07:14.273 に答える