如何在Java中制作连续键或索引

努尔法加尔之子

我有两个按整数顺序排列的列表索引,它们以id1初始化,第二个索引是id2两个索引都具有键/整数数,如下所示:

id1 = 1,1,1,2,2,3

id2 = 2,3,4,3,4,4

我想将两个索引都排序为

索引1,1,1,2,2,2,3,3,3,4,4,4

我如何到达?

这是我的代码:

    int d = list.size();

    int index = 0;                 

    List<Double> listM = new ArrayList<Double>();
    Map<Integer, Set<Integer>> data = new HashMap<Integer, Set<Integer>>();

    for (int id1 = 0; id1 < d - 1; id1++) {
            for (int id2 = id1 + 1; d2 < id; d2++) {

                 if (!data.containsKey(d1)) {
                    data.put(d1, new HashSet<Integer>());
                    }
                    data.get(d1).add(index);

                 if (!data.containsKey(d2)) {
                    data.put(d2, new HashSet<Integer>());
                 }
                 data.get(d2).add(index2);

            }

     }

     int cc = 0;
     for (Map.Entry<Integer, Set<Integer>> entry : data.entrySet()) {
         int maxIndex = 0;
         double maxScore = Double.MIN_VALUE;

         for (Integer index : entry.getValue()) {
             Double score = listM3.get(cc);                   

             if (score > maxScore) {
                 maxScore = score;
                 maxIndex = index;
             }
             cc++;
         }
     }

结果是

1,2,1,3,1,4,2,3,2,4,3,4,4

任何答复将不胜感激,谢谢。

奇闻趣事

您可以利用Collections.sort和进行类似的操作List.addAll

List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
List<Integer> combinedSortedList = new ArrayList<>();
combinedSortedList.addAll(list1);  //Add list1 to our new sorted list
combinedSortedList.addAll(list2); //Add list2 to our new sorted list
Collections.sort(combinedSortedList); //Sorts the list for you

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章