views:

413

answers:

6

Hello,

Is it possible to serialize a TreeMap with a comparator??

I've tested and it serializes well a treemap without comparator, when you add the comparator, it throws an exception.

If I declare comparator as transient, it still doesn't work. It only works if I make every tree map transient but it doesnt serialize the trees in that case.

+5  A: 

All the classes which you attempt to serialize must implement the java.io.Serializable interface. Also, each member variable in your class should be Serializable. In fact, your whole hierarchy should be.

Bozho
this **must** be the correct answer. http://java.sun.com/j2se/1.4.2/docs/api/java/io/NotSerializableException.html
pstanton
that might be the problem, I've gogoled a bit and don't know if treemap with comparator is serializable
d0pe
TreeMap is Serializable. Look elsewhere.
Bozho
treemap is but treemap with comparator is not. Thats what's killing me. I've serialized with the comparator and doesn't work, without it works fine.
d0pe
make your comparator implement Serializable. declare it as a private class, instead of an anonymous clas.
Bozho
A: 

They all do, it gives error in this classe.

martin
martin, reply to an answer by commenting on it, not by adding your own answer.
pstanton
I think Martin can't comment, as he/she doesn't have enough reputation.
Yuval
He can comment on answers to questions he asked himself
Jorn
A: 

For more help, look at this. Other things to consider (as mentioned in this document and elsewhere):

  • versioning
  • custom serial forms for objects
  • what fields of your object should be serialized and which ones are transient
Andy Gherna
it was martin's question.
pstanton
I read it and in forms it appears treemap as serializable as it is and field comparator in there but after some testing it doesn't work either.I was looking at this page:http://www.cs.chalmers.se/~catarina/java1.5/jsr-14-public-draft/collections-javadoc/serialized-form.html
d0pe
A: 

You might find that it is as simple as changing the Comparator field to a static rather than an instance member:

public final static Comparator<String> ID_IGN_CASE_COMP

You will need to make sure that all the other fields are Serializable but this should be a start.

Gareth Davis
It isn't because of it, comparator isn't serializable, but even if I declare it as transient, it still throws because of TreeMap that uses teh comparator.
d0pe
Then the answer is surely to make the Comparator Serializable.
Gareth Davis
A: 

Going from your comment on some other answers:

How can you declare the comparator transient? You can't modify the field in the TreeMap that saves it. What happens if you make the Comparator implement Serializable?

Jorn
Any reason for the downvote (after 5 months...)?
Jorn
A: 

TreeMap isn't doing anything special, here. If the only difference between a TreeMap that serializes and one that doesn't is your comparator... then your comparator is not serializable.

How are you implementing the comparator? Your transient declaration is meaningless to the TreeMap class which does not have a transient reference.

What does the exception say?

PSpeed