【问题标题】:Sorting Map<Integer,List<Double>>排序 Map<Integer,List<Double>>
【发布时间】:2014-02-05 06:58:24
【问题描述】:

基于Map&lt;Integer,List&lt;Double&gt;&gt; dist,我应该创建int[][] indexes,它将按升序存储排序索引,例如:

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

知道怎么做吗?谢谢。

【问题讨论】:

  • 到目前为止你做了什么?为什么你想要索引? Map 中没有这样的索引概念。
  • 我什至没有得到目标...顺便说一句,为什么indexes 是二维数组?
  • dist中的键可以有孔吗?另外,所有 List 的长度是否相同?
  • @Raffaele Rossi:因为我需要跟踪 dist 值的原始位置。在这种情况下, dist 有列和行。
  • @techG:不,列表未排序。但我可以对其进行排序。问题是我不知道如何基于 hashmap 创建一个二维排序数组。

标签: java sorting hashmap


【解决方案1】:

有可能吗。

  1. 遍历结构dist,
  2. 创建一个 RankedEntry 列表。

    公共类 RankedEntry 实现 Comparable{ 双倍价值; 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);
            }
        }
    
  3. 你在java中没有排序列表但是你可以使用Collections排序 方法。

  4. 然后你可以遍历它会给你的这个列表 结构。

  5. 获取rowIdx和colIdx并放入int[][] indexes

【讨论】:

  • 是的,但是如何排序呢?
  • 要删除重复项,您可以在步骤 2 中尝试 TreeSet。
【解决方案2】:

您可以创建TreeMap&lt;Double,List&lt;IndexClass&gt;&gt; 的反向映射。

 class IndexClass {
       public int row;
       public int column;
 }

而排序功能是这样的:

 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;
 }

然后你遍历 dist 并将它们放入 treeMap 中。

然后就可以打印出treeMap的值了。

【讨论】:

  • 您能说得更具体些吗?谢谢。
  • 是的,我很快就会写一个完整的例子。
  • 谢谢。只有一条评论: List 中可能有重复
  • 另外,您能否指定您在哪里对 List 值进行排序?
  • 嗨,我刚刚更新了答案。所以我们的想法是我们依靠 TreeMap 来为我们排序键。重复值将通过创建/重用 List 来解决。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 2014-12-19
  • 2022-09-30
  • 2014-05-04
  • 2015-06-06
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多