【问题标题】:Creating "ordinal" clusters based on List Integer elements in Java based on unique set基于唯一集在Java中基于List Integer元素创建“序数”集群
【发布时间】:2019-10-28 14:54:26
【问题描述】:

我正在尝试使用 List Integer 中的数字从 1 开始创建有序集群。

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

所以我想要实现的或多或少是集群的清理版本。唯一的例外是上述列表中的任何数字为 -1,在新的有序编号集群列表中将保持为 -1。

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

有了这个新集合,我想遍历 List Integer 并相应地更新它。因此,对于上面的示例,我将有:[-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 两次但只映射一次?

【问题讨论】:

  • 好的,这是我的问题。我不明白你实际上想要完成什么。获取唯一值的有序列表很简单,但之后就没有任何意义了。也许你从你的描述中比我理解得更好,但是如果你不能清楚地解释这个问题,那么尝试编码它将会是一团糟。
  • 发生的情况是我上面的值列表代表我的问题中的“集群”。但由于生成它们的初始代码并不重要,问题是,如果你想将问题呈现给某人并告诉有集群 45、90 等,我希望那些“集群”是“平滑的” ,那么他们可以问,中间集群在哪里?由于没有然后我创建唯一的数字集,并希望将此集替换为从 1 开始的有序数字,同时忽略列表中的数字 -1,即保持它们在新列表中的原样。 -1 有一个含义:未聚集。
  • {-1, 1, 2, 3, 4, 5, 6, 7, 8, 9} 获取的步骤 => ` [-1, 6, 9, 3, 4, 7, 5, 3, 8, 2, 6, 9, 1, 6 ]`不清楚
  • 另外,我在上面粘贴的输出只是为了可视化新列表上的分布,因此从中我可以看到有一个问题,原列表中的元素 159 不应该到那里。我不知道如何删除它,因为根据我的代码,它应该落在“集群”18 中,因此 18 有 2 个元素。
  • @Eritrean,我在示例中显示的基本上是我想将集合中的数字替换为从 1 开始的有序数字,但数字 -1 除外,即我保持原样...... . 然后在这样做之后,我再回到集合所基于的列表,并用新集合中的新数字替换数字......

标签: java arraylist grouping clustered-index


【解决方案1】:

问题出在这一行:

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

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

将该行更改为

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

【讨论】:

  • 非常感谢。不知道这个。我是 Java 新手 :-)
【解决方案2】:

最好为您的任务使用地图而不是列表,这会使代码更具可读性:

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

public class MainData {

    static Scanner scanner = new Scanner(System.in);

    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};
        //distinct values of your array collected to list
        List<Integer> myListInteger = Arrays.stream(myIntArray).boxed().distinct().sorted()
                                            .collect(Collectors.toList());

        System.out.println(myListInteger);

        //map your unique values to there index, except -1
        Map<Integer, Integer> indexToUniqueValue = new HashMap<>();
        indexToUniqueValue.put(-1, -1);

        for (int i = 1; i < myListInteger.size(); i++) {
            indexToUniqueValue.put(i, myListInteger.get(i));
        }

        System.out.println(indexToUniqueValue);

        //map the indexes to frequency in your original array
        Map<Integer, Integer> indexToFrequency = new HashMap<>();
        for (Map.Entry<Integer, Integer> entry : indexToUniqueValue.entrySet()) {
            indexToFrequency.put(entry.getKey(), countFreq(entry.getValue(), myIntArray));
        }
        System.out.println(indexToFrequency);
    }

    private static Integer countFreq(Integer value, int[] myIntArray) {
        int count = 0;
        for (int i : myIntArray) {
            if (i == value) {
                count++;
            }
        }
        return count;
    }
}

【讨论】:

  • 也感谢这个,非常整洁:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 2019-12-30
  • 2021-04-08
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
相关资源
最近更新 更多