在java中压缩整数数组 [英] Compressing array of integers in java
问题描述
我有一些非常大的整数数组,我想压缩。
但是在java中做的方法是使用这样的东西 -
I have some extremely large array of integers which i would like to compress.
However the way to do it in java is to use something like this -
int[] myIntArray;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new DeflaterOutputStream(byteArrayOutputStream));
objectOutputStream.writeObject(myIntArray);
请注意,int数组首先需要通过java转换为字节。
现在我知道这是快的,但它仍然需要创建一个全新的字节数组,扫描整个原始的int数组,将其转换为字节,并将值复制到新的字节数组。
Note that the int array first needs to be converted to bytes by java. Now I know that is fast but it still needs to create a whole new byte array and scan through the entire original int array converting it to bytes and copying the value to the new byte array.
有什么方法可以跳过字节转换,并立即压缩整数?
Is there any way to skip the byte conversion and make it compress the integers right away?
推荐答案
跳过 ObjectOutputStream
,只需将 int
直接存储为四个 byte
s。 DataOutputStream.writeInt
例如是一种简单的方法。
Skip the ObjectOutputStream
and just store the int
s directly as four byte
s each. DataOutputStream.writeInt
for instance is an easy way to do it.
这篇关于在java中压缩整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!