[英]Java SortedSet add all
The following code creates a sorted set that sorts by its values rather thank keys. 以下代码创建一个排序集,按其值排序,而不是感谢键。
vertexRank
is an object responsible for getting the value. vertexRank
是一个负责获取值的对象。 Everything works well except the code: vertexCentralities.addAll(vMap.entrySet());
除了代码之外,一切都运行良好:
vertexCentralities.addAll(vMap.entrySet());
What happens is that only the first entry from vMap is added to vertexCentralities rather than all entries. 会发生的是,只有vMap的第一个条目被添加到vertexCentralities而不是所有条目。
How can I get all entries from vMap into vertexCentralities? 如何从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());
I tried running: 我试过跑:
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());
}
}
and the output was sorted by value: 并且输出按值排序:
A: 70.50008784770233
Z: 55.48252329485239
E: 37.31308600830347
Y: 32.534528844628255
T: 16.544965680467794
R: 12.258316023552872
Maybe your problem comes from somewhere else... like g.getVertices()
or vertexRank.getVertexScore(vStr)
也许你的问题来自其他地方......比如
g.getVertices()
或者vertexRank.getVertexScore(vStr)
EDIT: I tried with duplicates values for the String
and for the double
: 编辑:我尝试使用
String
和double
重复值:
final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y", "A" };
curRank = new Random().nextInt(3);
and it looks like no duplicates are allowed. 并且看起来不允许重复。 Is this your problem?
这是你的问题吗?
EDIT: Found a solution if you want to allow multiple entry with the same Double
: Replace your SortedSet vertexCentralities
's comparator condition to: 编辑:如果你想允许多个条目使用相同的
Double
找到一个解决方案:将SortedSet vertexCentralities
的比较器条件替换为:
final int bValue = e2.getValue().compareTo(e1.getValue());
return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
An alternative solution could be to use SimpleEntry<K, V>
(inner public static class of java.util.AbstractMap) and avoid the SortedMap: 另一种解决方案可能是使用
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));
}
You should be able to have duplicate Key
or Value
but not both. 您应该能够拥有重复的
Key
或Value
但不能同时拥有两个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.