我有很多网址要处理.我在一个哈希集中存储了大约20'000'000.这会造成一些记忆问题.
我试图创建一个压缩的字符串类:
import java.io.*;//file writer
import java.util.*;
import java.util.zip.*;
class CompressedString2 implements Serializable{
private int originalSize;
private byte[] cstring;
public CompressedString2 (){
compress("");
}
public CompressedString2 (String string){
compress(string);
}
public void compress(String str){
try {
byte[] bytes = str.getBytes("UTF-8");
originalSize = bytes.length;
ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(deflatedBytes,new Deflater(Deflater.DEFAULT_COMPRESSION));
dos.write(bytes);
dos.finish();
cstring=deflatedBytes.toByteArray();
}catch(Exception e){e.printStackTrace();}
}
public String decompress() throws Exception{
String result="";
try{
ByteArrayOutputStream deflatedBytes=new ByteArrayOutputStream();
deflatedBytes.write(cstring);
deflatedBytes.close();
InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(deflatedBytes.toByteArray()));
byte[] …Run Code Online (Sandbox Code Playgroud)