Based on Map<Integer,List<Double>> dist
, I should create int[][] indexes
that will store sorted indexes in ascending order, eg:
dist =
0, [2.5, 3.5, 8.4]
1, [1.0, 5.0, 6.2]
int[][] indexes =
1,0 // refers to 1.0 in dist, because it's a smallest value
0,0, // refers to 2.5 in dist
0,1,
1,1,
1,2
0,2 // refers to 8.4 in dist
Any idea how to do this? Thanks.
you can create a reverse map of TreeMap<Double,List<IndexClass>>
.
class IndexClass {
public int row;
public int column;
}
And the sorting function is like this:
public List<IndexClass> sort(Map<Integer,List<Double>> dist) {
//Use TreeMap because it will auto sort keys
TreeMap<Double,List<IndexClass>> treeMap = new TreeMap<Double,List<IndexClass>>();
for (Map.Entry<Integer, List<Double>> entry : dist.entrySet()) {
List<Double> distValues = entry.getValue();
for (int i = 0; i < distValues.size(); i++) {
Double value = distValues.get(i);
IndexClass index = new IndexClass();
index.row = entry.getKey();
index.column = i;
//There is a duplicate value, simple add the index into the list
if (treeMap.contains(value) {
treeMap.get(value).add(index);
//It is a new value, so create a list instead
} else {
List<IndexClass> indexList = new ArrayList<IndexClass>();
indexList.add(index);
treeMap.put(value, indexList.add);
}
}
}
List<IndexClass> finalList = new ArrayList<IndexClass>();
//Add all list items together
for (Map.Entry<Double,List<IndexClass>> entry : treeMap.entrySet()) {
finalList.addAll(entry.getValue());
}
return finalList;
}
And then you loop through dist and put them into the treeMap.
Then you can print out the values of the treeMap.
Could It be possible.
Create a List of RankedEntry.
public class RankedEntry implements Comparable{ Double value; int rowIdx; int colIdx;
public RankedEntry(Double value, int rowIdx, int colIdx) { this.value = value; this.rowIdx = rowIdx; this.colIdx = colIdx; } @Override public int compareTo(Object o) { return this.value.compareTo((Double)o); } }
You do not have a sorted list in java but you can use Collections
sorting methods.
Then you can iterate through the This list it will give you structure.
Fetch rowIdx and colIdx and put them in int[][] indexes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.