user8954282
user8954282

Reputation:

How to serialize a HashMap with BufferedOutputStream in Java?

I have a very large HashMap of the format HashMap<String, List<String>>, and I want to serialize it using BufferedOutputStream because I think that it will be more efficient than with a regular OutputStream.

But how do I divide the HashMap in chunks of the size of the buffer? Should I just iterate through the HashMap?

Upvotes: 2

Views: 1072

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

If you plan to write into a local file you need to chain FileOutputStream, BufferedOutputStream and ObjectOutputStream. With below setup BufferedOutputStream should minimize direct writes to the file system using default buffer of 8192 bytes.

Map<String, List<String>> data = new HashMap<>();
data.put("myKey", List.of("A", "B", "C"));

File outFile = new File("out.bin");
try (FileOutputStream fos = new FileOutputStream(outFile);
     BufferedOutputStream bos = new BufferedOutputStream(fos);
     ObjectOutputStream oos = new ObjectOutputStream(bos)) {
    oos.writeObject(data);
    oos.flush();
}

Unless the output file is too big there is no need for further chunking.

Upvotes: 1

Related Questions