我有一个长度为4 int[]的大型数据集,我想计算4个整数的每个特定组合出现的次数。这与计算文档中的单词频率非常相似。

我想创建一个Map<int[],双>,将每个int[]映射到一个运行计数,因为列表是迭代的,但Map不接受基本类型。

所以我让Map<Integer[], Double>。

我的数据存储为ArrayList<int[]>,所以我的循环应该是这样的:

ArrayList<int[]> data = ... // load a dataset`

Map<Integer[], Double> frequencies = new HashMap<Integer[], Double>();

for(int[] q : data) {

    // **DO SOMETHING TO convert q from int[] to Integer[] so I can put it in the map

    if(frequencies.containsKey(q)) {
    frequencies.put(q, tfs.get(q) + p);
    } else {
        frequencies.put(q, p);
    }
}

我不确定在注释中需要什么代码才能将int[]转换为Integer[]。或者我根本不知道正确的方法是什么。


当前回答

正确的解决方案是使用这个类作为包装实际int[]的映射中的键。

public class IntArrayWrapper {
    int[] data;

    public IntArrayWrapper(int[] data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        IntArrayWrapper that = (IntArrayWrapper) o;

        if (!Arrays.equals(data, that.data))
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return data != null ? Arrays.hashCode(data) : 0;
    }
}

然后像这样修改代码:

Map<IntArrayWrapper, Double > freqs = new HashMap<IntArrayWrapper, Double>();

for (int[] data : datas) {
    IntArrayWrapper wrapper = new IntArrayWrapper(data);

    if (freqs.containsKey(wrapper)) {
        freqs.put(wrapper, freqs.get(wrapper) + p);
    }

    freqs.put(wrapper, p);
}

其他回答

正确的解决方案是使用这个类作为包装实际int[]的映射中的键。

public class IntArrayWrapper {
    int[] data;

    public IntArrayWrapper(int[] data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        IntArrayWrapper that = (IntArrayWrapper) o;

        if (!Arrays.equals(data, that.data))
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return data != null ? Arrays.hashCode(data) : 0;
    }
}

然后像这样修改代码:

Map<IntArrayWrapper, Double > freqs = new HashMap<IntArrayWrapper, Double>();

for (int[] data : datas) {
    IntArrayWrapper wrapper = new IntArrayWrapper(data);

    if (freqs.containsKey(wrapper)) {
        freqs.put(wrapper, freqs.get(wrapper) + p);
    }

    freqs.put(wrapper, p);
}

在没有外部库的情况下使用常规for循环:

将int[]转换为Integer[]:

int[] primitiveArray = {1, 2, 3, 4, 5};
Integer[] objectArray = new Integer[primitiveArray.length];

for(int ctr = 0; ctr < primitiveArray.length; ctr++) {
    objectArray[ctr] = Integer.valueOf(primitiveArray[ctr]); // returns Integer value
}

将Integer[]转换为int[]:

Integer[] objectArray = {1, 2, 3, 4, 5};
int[] primitiveArray = new int[objectArray.length];

for(int ctr = 0; ctr < objectArray.length; ctr++) {
    primitiveArray[ctr] = objectArray[ctr].intValue(); // returns int value
}

将int[]转换为Integer[]:

    import java.util.Arrays;
    ...

    int[] aint = {1,2,3,4,5,6,7,8,9,10};
    Integer[] aInt = new Integer[aint.length];

    Arrays.setAll(aInt, i -> aint[i]);

Int是一个原语。原语不能接受空值,只能有默认值。因此,要接受Null,您需要使用包装器类Integer。

选项1:

int[] nos = { 1, 2, 3, 4, 5 };
Integer[] nosWrapped = Arrays.stream(nos) 
                             .boxed()
                             .toArray(Integer[]::new);
nosWrapped[5] = null // can store null

选项2: 您可以使用任何使用包装器类Integer的数据结构

int[] nos = { 1, 2, 3, 4, 5 };
List<Integer> = Arrays.asList(nos)

Convert int[] to Integer[] public static Integer[] toConvertInteger(int[] ids) { Integer[] newArray = new Integer[ids.length]; for (int i = 0; i < ids.length; i++) { newArray[i] = Integer.valueOf(ids[i]); } return newArray; } Convert Integer[] to int[] public static int[] toint(Integer[] WrapperArray) { int[] newArray = new int[WrapperArray.length]; for (int i = 0; i < WrapperArray.length; i++) { newArray[i] = WrapperArray[i].intValue(); } return newArray; }