2012-12-06 39 views
1

以下代碼創建一個按照值排序的排序集,而不是按鍵。 vertexRank是負責獲取價值的對象。除了代碼之外,一切正常:vertexCentralities.addAll(vMap.entrySet());會發生什麼情況是,只有vMap的第一個條目被添加到vertexCentralities而不是所有條目。Java SortedSet全部添加

  1. 如何從vMap獲取所有條目到vertexCentralities中?

    SortedSet<Map.Entry<String, Double>> vertexCentralities = 
         new TreeSet<Map.Entry<String, Double>>(
         new Comparator<Map.Entry<String, Double>>() 
         { 
          @Override 
          public int compare(Map.Entry<String, Double> e1, Map.Entry<String, Double> e2) 
          { 
           return e2.getValue().compareTo(e1.getValue()); 
          } 
         }); 
    SortedMap<String, Double> vMap = new TreeMap<String, Double>(); 
    double curRank = 0; 
    for(String vStr: g.getVertices()) 
    { 
        curRank = vertexRank.getVertexScore(vStr); 
        vMap.put(vStr, curRank); 
    } 
    
    vertexCentralities.addAll(vMap.entrySet()); 
    
+1

在vMap上執行'sysout'來查看它有多少個元素? – mtk

回答

4

我試圖運行:

public static final void main(final String[] args) { 
    final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y" }; // init 

    final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() { 
     @Override 
     public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) { 
      return e2.getValue().compareTo(e1.getValue()); 
     } 
    }); 
    final SortedMap<String, Double> vMap = new TreeMap<String, Double>(); 
    double curRank = 0; 
    for (final String vStr : vStrs) { 
     curRank = new Random().nextDouble() * 100.0; // replacing 
                 // vertexRank.getVertexScore(vStr); 
                 // for testing 
     vMap.put(vStr, curRank); 
    } 
    vertexCentralities.addAll(vMap.entrySet()); 

    for (final Map.Entry<String, Double> entry : vertexCentralities) { 
     System.out.println(entry.getKey() + ": " + entry.getValue()); 
    } 

} 

和輸出是由值排序:

A: 70.50008784770233 
Z: 55.48252329485239 
E: 37.31308600830347 
Y: 32.534528844628255 
T: 16.544965680467794 
R: 12.258316023552872 

也許你的問題來自別的地方...像g.getVertices()vertexRank.getVertexScore(vStr)

編輯: 我試圖爲String重複值,併爲double

final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y", "A" }; 
curRank = new Random().nextInt(3); 

,它看起來像沒有允許重複。這是你的問題嗎?

編輯: 發現,如果你想允許使用相同的Double多次入境的解決方案: 替換您SortedSet vertexCentralities的比較條件:

final int bValue = e2.getValue().compareTo(e1.getValue()); 
return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey()); 
+0

哇!這很奇妙,你知道我的實際問題!這是重複的。 – CodeKingPlusPlus

0

另一個解決辦法是使用SimpleEntry<K, V>(內公開靜態類java.util.AbstractMap)和避免的SortedMap:

final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() { 
    @Override 
    public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) { 
     final int bValue = e2.getValue().compareTo(e1.getValue()); 
     return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey()); 
    } 
}); 
double curRank = 0; 
for (final String vStr : g.getVertices()) { 
    curRank = vertexRank.getVertexScore(vStr); 
    vertexCentralities.add(new SimpleEntry<String, Double>(vStr, curRank)); 
} 

你應該能夠有重複KeyValue,但不是兩者。