我有一个长度为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);
}
不用自己写代码,你可以使用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);
}
}
我不知道为什么你的地图上需要一个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 }));
在没有外部库的情况下使用常规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
}
本机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[]转换为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)