Reputation: 16081
The following code creates a sorted set that sorts by its values rather thank keys. vertexRank
is an object responsible for getting the value. Everything works well except the code: vertexCentralities.addAll(vMap.entrySet());
What happens is that only the first entry from vMap is added to vertexCentralities rather than all entries.
How can I get all entries from vMap into 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());
Upvotes: 1
Views: 2754
Reputation: 1537
An alternative solution could be to use SimpleEntry<K, V>
(inner public static class of java.util.AbstractMap) and avoid the 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.
Upvotes: 0
Reputation: 1537
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)
EDIT:
I tried with duplicates values for the String
and for the 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:
final int bValue = e2.getValue().compareTo(e1.getValue());
return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
Upvotes: 4