我有一个长度为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);
}

其他回答

虽然下面的代码进行了编译,但它在运行时抛出一个ArrayStoreException。


将int[]转换为Integer[]:

int[] old;
...
Integer[] arr = new Integer[old.length];
System.arraycopy(old, 0, arr, 0, old.length);

我必须承认,我有点惊讶,这编译,给定的系统。Arraycopy是低级别的,但它确实是。至少在Java 7中是这样。

你可以用另一种方式转换。

正确的解决方案是使用这个类作为包装实际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);
}

本机Java 8(一行)

在Java 8中,int[]可以很容易地转换为Integer[]:

int[] data = {1,2,3,4,5,6,7,8,9,10};

// To boxed array
Integer[] what = Arrays.stream( data ).boxed().toArray( Integer[]::new );
Integer[] ever = IntStream.of( data ).boxed().toArray( Integer[]::new );

// To boxed list
List<Integer> you  = Arrays.stream( data ).boxed().collect( Collectors.toList() );
List<Integer> like = IntStream.of( data ).boxed().collect( Collectors.toList() );

正如其他人所说,Integer[]通常不是一个好的映射键。 但是就转换而言,我们现在有了一个相对干净的本地代码。

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)

你不需要它。Int[]是一个对象,可以用作映射中的键。

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

是频率映射的正确定义。

这是错误的:-)。正确的解决方案也贴出来了:-)。