Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the serialization of Guava immutable collections stable?

I was warned against using Guava immutable collections in objects used in serialized communication because if the version of Guava on one end were updated, there could be serialization version incompatibility issues. Is this a valid concern?

like image 588
Kevin Wong Avatar asked Nov 29 '11 16:11

Kevin Wong


People also ask

Is collection serializable in Java?

In Java, the Collection-interfaces do not extend Serializable for several good reasons. In addition, most common implementations of these interfaces implement Serializable.

Is HashSet serializable Java?

Use HashSet that are implementing Set and HashSets are serializable. Just be sure that all the objects in the Set is serializable. For more info Why java. util.

Is Immutable List ordered?

Immutable List Static Factory Methods. The List. of static factory methods provide a convenient way to create immutable lists. A list is an ordered collection, where duplicate elements are typically allowed.

What are immutable collections in Java?

Immutable collections are the collections which can not be modified once they are created. Java 9 has introduced some static factory methods to easily create immutable collections like List, Set and Map. Before Java 9, wrapper methods of Collections class are used to create, not immutable, but unmodifiable collections.


2 Answers

Let's give some perspective.

The most prominent uses of serialization are:

  1. storing data in between runs of your application
  2. sending data between a client and a server

Guava is totally fine for application 2, if you control which Guava version is being used on both the client and the server. Moreover, while Guava doesn't make guarantees on the consistency of serialization between Guava versions...in reality, the serialized forms don't change very often.

On the other hand, let me give some perspective as to why Guava doesn't guarantee the consistency of serialized forms. I changed the serialized form of ImmutableMultiset between Guava releases 9 and 10, and the reason why was because I needed to refactor things so I could add ImmutableSortedMultiset to the immutable collections. You can see the change yourself here. Trying to do this same refactoring while keeping the serialized forms consistent would have almost certainly required additional awkward hacks, which are...pretty strongly against the Guava team's philosophy. (It might have been doable by a more expert programmer than myself, but I still claim it wouldn't have been trivial.) Guaranteeing serialization compatibility for the long term would have required staggering amounts of effort, as discussed in the above linked mailing list thread, Kevin stated:

Trying to provide for cross-version compatibility made things a hundred times more difficult and we gave up on it before Guava even started.

and Jared:

The underlying problem is still there: ensuring that the serialized forms are compatible between all Guava versions. That was a goal of mine when working towards Google Collections 1.0, but I abandoned that goal after realizing its difficulty. Implementing and testing cross-version compatibility wasn't worth the effort.

Finally, I'll point out that Guava gets used internally at Google all over the place and manages pretty well.

like image 170
Louis Wasserman Avatar answered Oct 06 '22 00:10

Louis Wasserman


Yes, that is a valid concern.

From the Guava project homepage (http://code.google.com/p/guava-libraries/):

Serialized forms of ALL objects are subject to change. Do not persist these and assume they can be read by a future version of the library.

If you're using Java native serialization Guava is not a good choice.

like image 22
Mike Deck Avatar answered Oct 06 '22 01:10

Mike Deck