我有一个长度为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[]。或者我根本不知道正确的方法是什么。
不用自己写代码,你可以使用IntBuffer来包装现有的int[],而不必将数据复制到Integer数组中:
int[] a = {1, 2, 3, 4};
IntBuffer b = IntBuffer.wrap(a);
IntBuffer的实现类似,因此您可以使用已经编写的代码。形式上,映射比较键,例如a.equals(b)用于表示两个键相等,因此两个具有数组1、2、3的intbuffer(即使数组位于不同的内存位置)被认为相等,因此将适用于频率代码。
ArrayList<int[]> data = ... // Load a dataset`
Map<IntBuffer, Double> frequencies = new HashMap<IntBuffer, Double>();
for(int[] a : data) {
IntBuffer q = IntBuffer.wrap(a);
if(frequencies.containsKey(q)) {
frequencies.put(q, tfs.get(q) + p);
} else {
frequencies.put(q, 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);
}
本机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[]通常不是一个好的映射键。
但是就转换而言,我们现在有了一个相对干净的本地代码。