Quurks
Quurks

Reputation: 53

Fast way to Serialize a HashMap

Im searching for a fast and easy way to serialize HashMaps. I know the Object(Out|In)putStreams, but as far as I know they are rather slow. I tried to use GSON, but i dont like to specify the type for deserialization (As usual, who doesnt like the type erasion).

The reasion I dont use XStream: I have several huge (250k Elements) int arrays, which i have to serialize too. And i dont want the overhead of < int>< /int> for each element.

The Hashmaps are short (100-200 Elements).

Upvotes: 1

Views: 3338

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533492

Often the problem is in the serialization of the keys and values. Depending on what you have, you can write it like this

DataOutputStream dos = 
Map<K, V> map = 
dow.writeInt(map.size());
for(Entry<K, V> entry: map.entrySet()) {
    dos.writeXxxx(entry.getKey());
    dos.writeXxxx(entry.getValue());
}

Upvotes: 3

Related Questions