2013-03-27 64 views
1

我在排序HashMapsJava值有一些問題。 我的代碼是:Java中的HashMap值排序不回正確的順序

@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, ?>我轉換爲<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 

第一個是未排序的,第二個是排序d。 我的問題是,第二輸出不在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());我得到那個奇怪的命令。

+1

好的,這裏有什麼問題? – midhunhk 2013-03-27 13:50:32

+0

值應該排序DESC順序,但它是975,975,975,900,1037,1000,1000,1037不是它應該在的位置 – user1601401 2013-03-27 13:51:11

+0

如果您的排序順序搞亂了,比較器看起來像是錯了。 – midhunhk 2013-03-27 13:53:25

回答

2

你比較看起來並不像它符合specification

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

你爲什麼返回1當兩個條目是平等的嗎?

+0

嘗試過,但沒有發生,輸出是一樣的 – user1601401 2013-03-27 13:55:38

+0

我編輯了我的問題,請再次看到它 – user1601401 2013-03-27 13:59:31

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

當兩個值相等時,你實際上是在說一個比另一個更大......這沒有按」沒有什麼意義。如果密鑰可比較,則使用自然比較。

+0

我已經嘗試過,但輸出是相同的,除了缺少重複項 – user1601401 2013-03-27 13:54:34

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; 
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被轉換爲分揀工作就像一個魅力。