2011-12-08 92 views
3
public class A{ 

    TreeMap<String, Double> sortedPairList; 
    HashMap<String, Double> PairList = new HashMap<String, Double>(); 

    public static void main(String[] args) { 

    A p = new A(); 

    p.PairList.put("a00", 0.3920948902348); 
    p.PairList.put("a01", 0.4920948902348); 
    p.PairList.put("a02", 0.3420948902348); 
    p.PairList.put("a03", 0.5920948902348); 
    p.PairList.put("a04", 0.6720948902348); 
    p.PairList.put("a05", 0.3940948902348); 
    p.PairList.put("a06", 0.3920948902348); 
    p.PairList.put("a07", 0.9920948902348); 
    p.PairList.put("a08", 0.6920948902348); 
    p.PairList.put("a09", 0.7920948902348); 
    p.PairList.put("a10", 0.8820948902348); 
    p.PairList.put("a11", 0.1220948902348); 
    p.PairList.put("a12", 0.1920948902348); 
    p.PairList.put("a13", 0.4520948902348); 
    p.PairList.put("a14", 0.3434948902348); 
    p.PairList.put("a15", 0.5690948902348); 
    p.PairList.put("a16", 0.5920948902348); 
    p.PairList.put("a17", 0.8920948902348); 
    p.PairList.put("a18", 0.920948902348); 
    p.PairList.put("a19", 0.9820948902348); 
    p.PairList.put("a20", 0.1920948902348); 
    p.PairList.put("a21", 0.5920948902348); 
    p.PairList.put("a22", 0.3920948902348); 
    p.PairList.put("a23", 0.3920948902348); 

    p.sortPairList(p.PairList) ; 

    for(String s : p.sortedPairList.keySet()){ 
     System.out.println("key:: value: " + s + " ::"+p.sortedPairList.get(s)); 
    } 

}//end of main 


public void sortPairList(HashMap<String, Double> pairlist) { 
    ValueComparator comp = new ValueComparator(pairlist); 

    sortedPairList = new TreeMap<String, Double>(comp); 

    sortedPairList.putAll(pairlist); 


}// end of sortedPredicatePairList 

class ValueComparator implements Comparator<Object> { 

    Map<String, Double> temp; 

    public ValueComparator(Map<String, Double> base) { 
     this.temp = base; 
    } 

    public int compare(Object p1, Object p2) { 

     if ((Double) temp.get(p1) < (Double) temp.get(p2)) { 
      return 1; 
     } else if ((Double) temp.get(p1) == (Double) temp.get(p2)) { 
      return 0; 
     } else { 
      return -1; 
     } 

    } 
}// end of class ValueComparator 
}//end of classA 

*我得到的輸出是下面的,爲什麼我收到空在其中重複的值:*Java:爲什麼我在輸出中變爲null?

鍵::值:A07 :: 0.9920948902348

鍵::值:A19 :: 0.9820948902348

鍵::值:A18 :: 0.920948902348

鍵::值:A17 :: 0.8920948902348

鍵::值:A10 :: 0.8820948902348

鍵::值:A09 :: 0.7920948902348

鍵::值:A08 :: 0.6920948902348

鍵::值:A04 :: 0.6720948902348

鍵::值:A03 :: 0.5920948902348

鍵::值:A21 ::空

鍵::值:A16 ::空

鍵::值:A15 :: 0.5690948902348

鍵::值:A01 :: 0.4920948902348

鍵::值:A13 :: 0.4520948902348

鍵::值:A05 :: 0.3940948902348

鍵::值:A06 :: 0.3920948902348

鍵::值:A23 :: 0.3920948902348

鍵::值:A22 :: 0.3920948902348

鍵::值:A00 ::空

鍵::值:A14 :: 0.3434948902348

鍵::值:A02 :: 0.3420948902348

鍵:: v ALUE:A12 :: 0.1920948902348

鍵::值:A20 ::空

鍵::值:A11 :: 0.1220948902348

回答

3

Tree Map API

注意,訂貨維持通過有序映射(無論是否提供顯式比較器)必須與equals相等,如果此有序映射要正確實現Map接口。(請參閱Comparable或Comparator以獲得與equals一致的精確定義。)這是因爲Map接口是根據equals操作定義的,但是一個map使用其compareTo(或compare)方法執行所有關鍵比較,所以兩個關鍵字從排序地圖的角度來看,這種方法被認爲是相等的。

您的ValueComparator肯定不等於等於。

不知怎的,p.sortedPairList.keySet()包含根據您的「ValueComparator」「相等」的鍵,但重複鍵得到了null

+0

但我沒能看到我比較錯誤,請明確告訴è什麼是錯的比較器的代碼? – lancelot

+0

查看Tree Map API的鏈接,仔細閱讀「Compare()與Equals()不一致」的含義。換句話說,您的TreeMap中的鍵由ValueComparator.compare()方法「相等」,但不等於String.equals()方法 – korifey

2

聲明PairList這樣的:

HashMap<String, Double> PairList = new HashMap<String, Double>(); 

,改變你的ValueComparator#compare方法:

public int compare(Object p1, Object p2) { 
    return temp.get(p1).compareTo(temp.get(p2)); 
} 

更新根據您的意見使用以下compare方法獲取升序排列所有的24個元素:

public int compare(String p1, String p2) { 
    if (temp.get(p1).doubleValue() < temp.get(p2).doubleValue()) 
     return 1; 
    else if (temp.get(p1).doubleValue() == temp.get(p2).doubleValue()) 
     return p1.compareTo(p2); 
    else 
     return -1; 
} 
+0

這可以工作但升序,我在尋找降序.... – lancelot

+0

好的得到它..這工作..謝謝.... – lancelot

+0

'return temp.get(p1).compareTo(temp.get(p2));'給我**降序**順序。 – anubhava

0

錯誤在你的比較器中。

在java中,您無法使用==運算符比較對象。這個操作符做你所期望的。因此,要比較您在地圖中保存的雙打值,您必須致電doubleValue()。這是我的比較器版本,可以正常工作。

public int compare(Object p1, Object p2) { 

     if (temp.get(p1).doubleValue() < temp.get(p2).doubleValue()) { 
      return 1; 
     } else if (temp.get(p1).doubleValue() == temp.get(p2).doubleValue()) { 
      return 0; 
     } else { 
      return -1; 
     }   
    } 
+0

對不起,這是給錯誤..... – lancelot

0

這是因爲你在你的比較與==跡象比較。替換與equals()方法,它應該工作:

public int compare(Object p1, Object p2) { 

      if ((Double) temp.get(p1) < (Double) temp.get(p2)) { 
       return 1; 
      } else if (((Double) temp.get(p1)).equals((Double) temp.get(p2))) { 
       return 0; 
      } else { 
       return -1; 
      } 

     } 
+0

但現在這是給另一個問題,我沒有得到重複值,只有單個值正在打印。 它只打印18個鍵值對而不是24個 – lancelot

相關問題