我有一个长度为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是一个原语。原语不能接受空值,只能有默认值。因此,要接受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>();

是频率映射的正确定义。

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

如果你想将int[]转换为Integer[],在JDK中没有自动的方法。然而,你可以这样做:

int[] oldArray;

... // Here you would assign and fill oldArray

Integer[] newArray = new Integer[oldArray.length];
int i = 0;
for (int value : oldArray) {
    newArray[i++] = Integer.valueOf(value);
}

如果你可以访问Apache的lang库,那么你可以像这样使用ArrayUtils.toObject(int[])方法:

Integer[] newArray = ArrayUtils.toObject(oldArray);

假设您希望映射的键根据元素的值匹配,而不是数组的标识。在这种情况下,您需要像您所期望的那样定义equals和hashCode的某种对象。最简单的是转换为List<Integer>,要么是ArrayList,要么更好的使用Arrays.asList。更好的做法是引入一个表示数据的类(类似于java.awt.Rectangle,但我建议将变量设置为private final,类也设置为final)。

我不知道为什么你的地图上需要一个Double。就您正在尝试做的事情而言,您有一个int[],您只想要每个序列出现的次数(?)。为什么这需要双份?

我将使用适当的.equals和. hashcode方法为int数组创建一个包装器,以说明int[]对象本身并不考虑这些方法版本中的数据。

public class IntArrayWrapper {
    private int values[];

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

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(values);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        IntArrayWrapper other = (IntArrayWrapper) obj;
        if (!Arrays.equals(values, other.values))
            return false;
        return true;
    }

}

然后使用谷歌Guava的multiset,只要您放入其中的元素类型具有适当的.equals和. hashcode方法,它就完全用于计数出现次数。

List<int[]> list = ...;
HashMultiset<IntArrayWrapper> multiset = HashMultiset.create();
for (int values[] : list) {
    multiset.add(new IntArrayWrapper(values));
}

然后,为了获得任何特定组合的计数:

int cnt = multiset.count(new IntArrayWrapper(new int[] { 0, 1, 2, 3 }));

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