java - Java中基于唯一集的List Integer元素创建 "ordinal"簇

标签 java arraylist grouping clustered-index

我正在尝试使用列表整数中的数字创建从 1 开始的有序簇。

例如,如果我有一个列表整数,例如:[-1, 7, 99, 4, 5, 33, 6, 4, 77, 3, 7, 99, 2, 7],这些数字是返回的簇通过算法。该算法不会创建像 1、2、3... 这样的连续编号,而是随机“跳跃”。

所以我想要实现的或多或少是集群的清理版本。唯一的异常(exception)是上面列表中任何为 -1 的数字,在新的有序编号簇列表中仍将保留 -1。

为了说明这一点,假设上面的列表,我为这些独特的簇创建了一组独特的元素:{-1, 2, 3, 4, 5, 6, 7, 33, 77, 99},我想创建新的编号,例如将集合更改为 {-1, 1, 2, 3, 4, 5, 6, 7, 8, 9} 以替换之前的集合,同时保持 -1 不变。前一组中的每个索引都对应于新组中的索引。

有了这个新集合,我想运行列表整数并相应地更新它。因此,对于上面的示例,我将具有:[-1, 6, 9, 3, 4, 7, 5, 3, 8, 2, 6, 9, 1, 6]。

到目前为止我做了什么?

import java.util.*;

public class testing {
    public static void main(String[] args) {

    int[] myIntArray = new int[]{-1, 1, 2, 3, 4, 5, 5, -1, 7, 5, 9, 5, 5, 10,
            4, 14, -1, 5, 5, 5, 5, 5, 14, 5, 22, 5, 5, 25, 5, 22, 22, 5, 5, 5, 4, 5, 4, 7, 5, 5, 14, 14, 5,
            5, 22, 9, 2, 5, 22, -1, 47, 5, 5, 5, 5, 5, 4, -1, -1, 5, 5, 22, 5, 5, 5, 9, 5, 5, 5, 5, 65, 5,
            5, 5, 5, 14, 5, 10, 5, -1, 5, 22, 5, 14, 14, 5, 5, 5, 5, 5, 22, 5, 5, 5, 5, 5, -1, -1, 90, 22,
            -1, 92, 47, -1, 65, -1, 47, -1, 5, 1, -1, 7, 47, 92, -1, 9, -1, 9, -1, 103, 47, 3, 14, 107, 1,
            92, -1, 4, -1, 4, 14, -1, 9, -1, -1, 22, -1, 9, 22, 92, 25, 92, 9, 14, -1, 92, 103, 47, 4, -1,
            22, 9, 92, 47, -1, 47, 9, 7, 107, -1, -1, 47, 9, 14, 4, 47, -1, 22, 4, 22, 9, 9, 90, -1, -1, 4,
            4, 22, 22, 103, 47, 47, -1, -1, 9, 14, 9, 4, 4, 22, 22, 159, 9, 103, 4, 22, 4, 159, 90, 4};

    List<Integer> myListInteger = new ArrayList<Integer>(myIntArray.length);

    // passing values to myListInteger from myIntArray
    for (int i : myIntArray) {
        myListInteger.add(i);
    }

    // get distinct numbers in myListInteger: Set
    Set<Integer> distinctNumbersSet = new HashSet<Integer>(myListInteger);

    // convert to List
    List<Integer> distinctIntegerList = new ArrayList<>();
    for (Integer i: distinctNumbersSet) {
        distinctIntegerList.add(i);
    }

    // index to start numbering unique values
    int index = 1;
    boolean increaseIndex = false;


    for (int i = 0; i < distinctIntegerList.size(); i++) {
        for (int j = 0; j < myListInteger.size(); j++ ) {
            if (myListInteger.get(j) == -1) {
                continue;
            }

            if (distinctIntegerList.get(i) == myListInteger.get(j)) {
                myListInteger.set(j, index);
                increaseIndex = true;
                continue;
            }
        }
        if (increaseIndex == true) {
            index++;
            increaseIndex = false;
        }

    }

    // after update the myListInteger, I can get distinct sets again
    Set<Integer> distinctSetAfterUpdate = new HashSet<Integer>(myListInteger);

    System.out.println(myListInteger); // there is a 159 almost at the end, while it is expected that it should be 18

    for (Integer ind: distinctSetAfterUpdate) {
        System.out.println(ind + ": " +  Collections.frequency(myListInteger, ind));
    }



    }
}

我遇到的问题

列表中最高的簇:出现两次的 159,不会进入新的簇 18...如果我尝试可视化新映射上的分布,不知何故,此 159 显示为具有 1 个值的簇,而 18 则显示为1 也是...,虽然根据我在代码中的逻辑,这个新的簇映射永远不应该超过集合的大小。

所以我当前用于可视化分布的输出是:

-1: 33
1: 3
2: 2
3: 2
4: 17
5: 56
6: 4
7: 16
8: 2
9: 12
10: 19
11: 2
12: 12
13: 2
14: 3
15: 7
16: 4
17: 2
18: 1
159: 1

虽然我想得到

-1: 33
1: 3
2: 2
3: 2
4: 17
5: 56
6: 4
7: 16
8: 2
9: 12
10: 19
11: 2
12: 12
13: 2
14: 3
15: 7
16: 4
17: 2
18: 2

有任何帮助试图理解为什么我的代码没有将 159 两次映射到 18 而只映射一次吗?

最佳答案

问题出在这一行:

if (distinctIntegerList.get(i) == myListInteger.get(j))

您的列表中有整数类型。 == 用于比较基本类型(int、long、double ..)。 比较引用类型(Integer、Double、Long)时应始终使用 equals 方法

将该行更改为

if (distinctIntegerList.get(i).equals(myListInteger.get(j)))

关于java - Java中基于唯一集的List Integer元素创建 "ordinal"簇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593205/

相关文章:

java - 更新 ArrayList 中匹配字母的所有实例

java - 为什么我无法将对象添加到我的 arrayList 中?

MySQL:如何从一个 GROUP BY 级别中选择一个值并在所有级别中广播

java - Websphere 7 错误 : A WSDL Definition could not be generated for the implementation class

java - 为什么我的 thymeleaf 重定向不起作用?

java - 尝试读取 json 文件时出现 JsonParserException

java - 如何在 if else 条件下使用 firebase 查询

java - 删除数组列表中的元素

string - 如何在 Scala 字符串中找到 Id 的出现

r - 按多列分组并对其他多列求和