kryo icon indicating copy to clipboard operation
kryo copied to clipboard

Saving and loading the results of HashMap.keySet()

Open andycarlsonuk opened this issue 8 years ago • 7 comments

I've hit an issue today because my application stores some objects that are obtained from the keySet() method of java.util.HashMap.

These objects are instances of the HashMap.KeySet inner class. I can persuade Kryo to save them but it won't load them back because construction of an instance requires a reference to the 'outer' HashMap. The JDK class does not seem to provide any way to get hold of this reference... which I think I would need if I was going to use a custom serializer to solve the problem.

The 'outer' map is not readily available at the time of serialization BTW.

At the moment the best answer I can think up is to use a custom serializer and create a plain old HashSet when loading back from the serialized form but this then loses the set's association with its parent, for example remove() on a KeySet instance will remove the entry from the 'owning' Map. It will probably do the job but it does not seem like a great answer.

Is there a better way?

Thanks.

andycarlsonuk avatar Sep 03 '18 14:09 andycarlsonuk

You only have two options:

  • Write a custom serializer that uses a HashSet on deserialization.
  • Serialize the entire map (store the map with your serialized objected).

There is no easy way for an application to determine the owning map of the result of HashMap.keySet() when given only the key set. As such, the map either needs to be given up front or you have to accept the loss of association.

isaki avatar Sep 03 '18 18:09 isaki

Thanks. I was hoping that there might be a more general way. I guess this problem is common to using Kryo with many inner classes. It's the second time I've hit it with Kryo but on the previous occasion it was easy enough to supply a reference to the outer object to the serializer via other means.

andycarlsonuk avatar Sep 03 '18 21:09 andycarlsonuk

The issue with inner classes is they rely on the outer object for state. Let's look at your example:

final class KeySet extends AbstractSet<K>

That is the declaration inside java.util.HashMap. By definition, since this is not marked static it cannot exist without the existence of an owning, outer object as its state is derived and backed by the outer class' object instance.

isaki avatar Sep 04 '18 14:09 isaki

Yes indeed.

I guess my frustration stems from the feeling that the reference must be there under the covers - each inner object must keep a reference to the outer object... but perhaps it is too well protected from access by anything outside of the implementing class so Kryo is unable to make use of it.

andycarlsonuk avatar Sep 04 '18 15:09 andycarlsonuk

In older versions of Java, you could do some really questionable things to access such information. I've never tried to do this personally, but I imagine you can't do things that violate the rules of the language in newer JDKs. Even if there were such a way, you would probably want to keep such fragile and absolutely unsupported code out of Kryo itself and only as a custom serializer within the code base that has to have it.

You could start by looking at this.

https://stackoverflow.com/questions/1816458/getting-hold-of-the-outer-class-object-from-the-inner-class-object

isaki avatar Sep 04 '18 19:09 isaki

OK so I could but I shouldn't.

The HashSet thing will do the job for me in this case.

So I don't need to and I won't.

Thanks again.

andycarlsonuk avatar Sep 04 '18 19:09 andycarlsonuk

No problem!

isaki avatar Sep 04 '18 21:09 isaki