本文介绍了Guava SetMultimap无法序列化(由于WrappedSet无法序列化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用Java序列化,这对于存储完整的对象层次结构非常有用.

I'm often using java serialization, which is very usefull to store a complete object hierarchy.

当尝试序列化SetMultimap时,我遇到一个异常,说 AbstractMultimap.WrappedSet 无法序列化.

When trying to serialize a SetMultimap, I got an exception saying that that AbstractMultimap.WrappedSet is not serializable.

番石榴用户如何解决此问题?

How do guava users workaround with this problem?

预先感谢

推荐答案

多图元素的视图(例如,从get方法返回的集合,asMap视图等) .)不可序列化.但是,并非如此,SetMultimap实现将无法序列化. Guava提供的SetMultimap的所有实现实际上都是可序列化的……只是它们的部分视图集合没有.

The views of elements of a multimap (such as the collections returned from get methods, the asMap view, etc.) are intentionally not serializable. However, it isn't true that a SetMultimap implementation would not be serializable because of that. All implementations of SetMultimap that Guava provides are in fact serializable... it's just the partial view collections for them that are not.

如果您需要序列化这些集合之一,则应将其显式复制到普通集合:

If you need to serialize one of these collections, you should explicitly copy it to a normal collection:

Set<Foo> foo = Sets.newHashSet(multimap.get(someKey));

这篇关于Guava SetMultimap无法序列化(由于WrappedSet无法序列化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:45