简体   繁体   中英

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).

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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