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