Reputation: 1
I can't seem to be able to serialize a TreeMap if I need to use a collator. There's really no info about it online and I can't seem to fix it by myself. Any help would be appreciated on how to get a Serializable Collator. Thanks.
Upvotes: 0
Views: 47
Reputation: 719229
This is what you need to do.
Implement a serializable collator class using the wrapper technique given in the answer to Serializing Collator instance. (What this is doing is holding the actual collator in a transient
field, and reconstructing it by calling Collator.getInstance
when the wrapper is deserialized.)
Create an instance of that class.
Instantiate the TreeMap
class passing the collator instance as the constructor argument.
You should now be able to serialize the TreeMap
.
Note that the TreeMap
comparator is held in a private
field that is NOT marked as transient
, and it is being serialized / deserialized by the map's private
writeObject
and readObject
methods. There is no clean way to override this behavior in TreeMap
.
The only other alternatives are to look for (or create) an implementation of tree maps where the comparator is marked as transient, or an implementation of Collator
that is serializable.
Referring to the above link, you said:
I've tried it and it didn't work.
It should work. Perhaps you can tell us how it didn't work. Did you get exceptions? Incorrect behavior?
Upvotes: 1