Java 分类图<;整数,列表<;双倍>&燃气轮机;
基于Java 分类图<;整数,列表<;双倍>&燃气轮机;,java,sorting,hashmap,Java,Sorting,Hashmap,基于Map dist,我应该创建int[][]索引,以升序存储已排序的索引,例如: 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
Map dist
,我应该创建int[][]索引
,以升序存储已排序的索引,例如:
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
你知道怎么做吗?谢谢。您可以创建
TreeMap
的反向映射
排序功能如下:
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;
}
公共列表排序(地图区){
//使用TreeMap,因为它将自动对键进行排序
TreeMap TreeMap=新的TreeMap();
对于(Map.Entry:dist.entrySet()){
List distValues=entry.getValue();
对于(int i=0;i
然后你在dist中循环,把它们放到树状图中
然后您可以打印出树状图的值。您可以创建一个
树状图的反向映射
排序功能如下:
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;
}
公共列表排序(地图区){
//使用TreeMap,因为它将自动对键进行排序
TreeMap TreeMap=新的TreeMap();
对于(Map.Entry:dist.entrySet()){
List distValues=entry.getValue();
对于(int i=0;i
然后你在dist中循环,把它们放到树状图中
然后您可以打印出树形图的值。可能吗
遍历结构域
创建RankedEntry的列表
公共类RankedEntry实现可比较{
双重价值;
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);
}
}
java中没有排序列表,但可以使用Collections
排序
方法
然后,您可以遍历它将提供给您的列表
结构
获取rowIdx和colIdx并将它们放入int[][]索引中
有可能吗
遍历结构域
创建RankedEntry的列表
公共类RankedEntry实现可比较{
双重价值;
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);
}
}
java中没有排序列表,但可以使用Collections
排序
方法
然后,您可以遍历它将提供给您的列表
结构
获取rowIdx和colIdx并将它们放入int[][]索引中
到目前为止你都做了些什么?你到底为什么想要索引?在地图中没有这样的索引概念。我甚至没有达到目标…顺便说一句,为什么索引是一个2d数组?在距离中的键会有洞吗?而且,所有的列表都是等长的吗?@Raffaele Rossi:因为我需要跟踪f dist值。在这种情况下dist有列和行。@techG:不,列表没有排序。但我可以排序。问题是我不知道如何基于hashmap创建2d排序数组。到目前为止,你做了什么?你为什么想要索引?在映射中没有这样的索引概念。我甚至没有达到目标…顺便说一句,为什么索引是2d数组吗?距离中的键是否会有洞?所有列表的长度是否都相等?@Raffaele Rossi:因为我需要跟踪距离值的原始位置。在这种情况下,距离有列和行。@techG:不,列表没有排序。但我可以排序。问题是我不知道如何创建我使用了一个基于hashmap的2d排序数组。你能说得更具体一点吗?谢谢。是的,我很快就会写一个完整的例子。谢谢。只需一句话:列表中可能有重复项。你能指定列表值的排序位置吗?嗨,我刚刚更新了答案。所以我们的想法是依靠TreeMap为我们排序键。还有duplicate值将通过创建/重用列表来解决。希望它对您有所帮助。您能更具体一点吗?谢谢。是的,我很快会写一个完整的示例。谢谢。只需一条注释:列表中可能有重复项。另外,您能指定列表值的排序位置吗?嗨,我刚刚更新了答案。所以我们的想法是在TreeMap上为我们排序。重复的值将通过创建/重用列表来解决。希望这对您有帮助。是的,但是如何排序?要删除重复项,您可以在步骤2中尝试TreeSet。是的,但是如何排序