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());
I tried running:
and the output was sorted by value:
Maybe your problem comes from somewhere else… like
g.getVertices()orvertexRank.getVertexScore(vStr)EDIT:
I tried with duplicates values for the
Stringand for thedouble: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: