1

以下代码创建了一个排序集,该集按其值而不是感谢键进行排序。vertexRank是负责获取值的对象。除了代码之外,一切都运行良好:vertexCentralities.addAll(vMap.entrySet());发生的情况是只有 vMap 的第一个条目被添加到 vertexCentralities,而不是所有条目。

  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());
    
4

2 回答 2

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());
于 2012-12-06T08:39:10.590 回答
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但不能两者兼有。

于 2012-12-06T09:27:58.333 回答