小编MAZ*_*DAK的帖子

kryo列表序列化

我正在尝试使用Kryo序列化一些对象列表(自定义类的列表:List>).

list2D; // List<List<MyClass>> which is already produced.

Kryo k1 = new Kryo();
Output output = new Output(new FileOutputStream("filename.ser"));
k1.writeObject(output, (List<List<Myclass>>) list2D);
output.close();
Run Code Online (Sandbox Code Playgroud)

到目前为止没问题,它写出了没有错误的列表.但是当我尝试阅读它时:

Kryo k2 = new Kryo();
Input listRead = new Input(new FileInputStream("filename.ser"));
List<List<Myclass>> my2DList = (List<List<Myclass>>) k2.readObject(listRead,  List.class);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Exception in thread "main" com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): java.util.List
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

java serialization constructor list kryo

10
推荐指数
2
解决办法
8704
查看次数

在大量迭代之后,Java while循环会随着时间的推移而显着减慢

我的程序在while循环中逐行读取文本文件.然后它处理每一行并提取要在输出中写入的一些信息.它在while循环中所做的一切都是O(1),除了两个我认为是O(N)的ArrayList indexOf()方法调用.该程序在开始时以合理的速度运行(每100秒1M行),但随着时间的推移它会大幅减速.我在输入文件中有70 M行,因此循环迭代7000万次.从理论上讲,这应该需要大约2个小时,但实际上需要13个小时.问题出在哪儿?

这是代码片段:

BufferedReader corpus = new BufferedReader(
            new InputStreamReader(
                        new FileInputStream("MyCorpus.txt"),"UTF8"));

Writer outputFile = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("output.txt"), "UTF-8"));

List<String> words = new ArrayList();
//words is being updated with relevant values here   

LinkedHashMap<String,Integer> DIC = new LinkedHashMap();
//DIC is being updated with relevant key-value pairs here    

String line = ""; 
while ((line = corpus.readLine()) != null)
    String[] parts = line.split(" ");
    if (DIC.containsKey(parts[0]) && DIC.containsKey(parts[1])) {

        int firstIndexPlusOne = words.indexOf(parts[0])+ 1;
        int secondIndexPlusOne = words.indexOf(parts[1]) +1;

        outputFile.write(firstIndexPlusOne …
Run Code Online (Sandbox Code Playgroud)

java time-complexity while-loop

6
推荐指数
1
解决办法
992
查看次数