London guy
London guy

Reputation: 28012

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

I have serialized a HashMap into a file using objectstream and fileoutputstream. It is a very huge HashMap with around 150 million entries. It takes a long time (~40 mins) to load when I read it back from the file.

I am using a FileOutputStream followed by ObjectOutputStream to serialize the object. Then, I am using the ObjectInputStream and FileInputStream to read the object.

Is there a recommended way to read a serialized HashMap so that it loads quickly from the file?

Upvotes: 0

Views: 1289

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

Using a BufferedInputStream should improve the performance:

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

And of course, a BufferedOutputStream would also improve the performance of the serialization.

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.

Upvotes: 6

Related Questions