Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava - Can a Multimap be serialized?

I am looking at this API ArrayListMultiMap which implements the Serializable interface. Does that mean I can serialize this object ? Are all Multimap objects serialized ?

like image 568
Princesh Avatar asked Dec 13 '12 15:12

Princesh


1 Answers

The meaning of Serializable is always the same: If an object isn't serializable, it can't be serialized. If it is, it may work or not... Especially in case of collections (including maps and multimaps), it depends on their content.

As an example, you can surely serialize ArrayList<String> as ArrayList.class is serializable and so is each member of the list. OTOH trying to serialize ArrayList<Object> may or may not work: If all contained objects are e.g. strings, it will work. If any member is not serializable, you'll get an exception.

Does it mean I can serialize this object?

If all keys and values are serializable, you can.

Are all multiMap object serializable?

No, the interface Multimap doesn't extend Serializable, so there may be non-serializable implementation. Indeed, you can get such an instance via e.g. Multimaps.filterEntries.

like image 151
maaartinus Avatar answered Sep 21 '22 23:09

maaartinus