简体   繁体   English

用Java加载序列化哈希图的最快方法是什么?

[英]What is the quickest way to load a serialized hashmap in Java?

I have serialized a HashMap into a file using objectstream and fileoutputstream. 我已经使用对象流和文件输出流将HashMap序列化为文件。 It is a very huge HashMap with around 150 million entries. 这是一个非常庞大的HashMap,大约有1.5亿个条目。 It takes a long time (~40 mins) to load when I read it back from the file. 从文件读回时,加载时间很长(约40分钟)。

I am using a FileOutputStream followed by ObjectOutputStream to serialize the object. 我正在使用FileOutputStream,然后使用ObjectOutputStream来序列化对象。 Then, I am using the ObjectInputStream and FileInputStream to read the object. 然后,我使用ObjectInputStream和FileInputStream读取对象。

Is there a recommended way to read a serialized HashMap so that it loads quickly from the file? 有没有推荐的方法来读取序列化的HashMap,以便从文件中快速加载?

Using a BufferedInputStream should improve the performance: 使用BufferedInputStream应该可以提高性能:

ObjectInputStream in = 
    new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));

And of course, a BufferedOutputStream would also improve the performance of the serialization. 当然,BufferedOutputStream也可以提高序列化的性能。

Using those buffered streams allow reading large chunks of bytes from the file system in one shot, instead of reading byte per byte. 使用这些缓冲的流允许一次性读取文件系统中的大字节字节,而不是每个字节读取字节。 Read the documentation for more information. 阅读文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM