尽管 harpun 的解决方案正常工作,但我想到了另一个问题.. 这个问题更多的是关于性能,因为我的应用程序进行了大量的int[]
写入和读取..
我认为以下解决方案应该更高效..
写:
WritableUtils.writeVInt(out, array.length);
for(int i=0; i<array.length; i++)
WritableUtils.writeVInt(out, array[i]);
读:
int[] array = new array[WritableUtils.readVInt(in)];
for(int i=0; i<array.length; i++)
array[i] = WritableUtils.readVInt(in);
而不是每次都将其包装成 IntArrayWritable ..
写:
IntWritable[] a = new IntWritable[array.length];
for(int i=0; i<a.length; i++)
a[i] = new IntWritable(array[i]);
IntArrayWritable arrayWritable = new IntArrayWritable();
arrayWritable.set(a);
arrayWritable.write(arg0);
读:
IntArrayWritable arrayWritable = new IntArrayWritable();
arrayWritable.readFields(arg0);
Writable[] a = arrayWritable.get();
int[] array = new int[a.length];
for(int i=0; i<array.length; i++)
array[i] = ((IntWritable)a[i]).get();
不是吗?你怎么看?