Search code examples
javahazelcastserializablein-memory-databasehazelcast-imap

Nested collections in Hazelcast IMap


I have a hazelcast instance and I am getting a map from it.

IMap<Object,Object> cache= hazelCastInstance.getMap(collectionName);

I want to store a nested collection (e.g. List < List< Definition > >) into it.

Definition => Class (This implements serializable)

cache.set(cacheKey, object, ttl,TimeUnit.SECONDS);

cacheKey => unique key

object => nested collection

ttl => time to live

I am getting an exception: java.io.NotSerializableException: java.util.ArrayList$SubList

I am using hazelcast for the first time. Does anyone have any idea why is that?

Please advice.

Thanks


Solution

  • The error is caused by the ArrayList$Sublist not being serializable. It can be resolved by replacing your code by something like this:

    cache.set(cacheKey, new ArrayList(objects), ttl, SECONDS)
    

    Now you got rid of the inner class that is causing problems. The content of the sublist will be copied into a normal ArrayList and there are no problems serializing that.