5 Replies Latest reply on Apr 24, 2012 7:03 PM by alexey.ragozin

    Serialize an ArrayList<ArrayList>

    833144
      Hi,

      I'm using an ArrayList<ArrayList<Integer>> but after the POF deserialization I can´t access the inner arraylists:

      java.lang.ClassCastException: com.tangosol.util.ImmutableArrayList$ListView cannot be cast to java.util.ArrayList


      I´m doing de readExternal/writeExternal this way:

      list = (ArrayList<ArrayList<Integer>>)reader.readCollection(1, new ArrayList<ArrayList<Integer>>())
      writer.writeCollection(1, list);


      Any clue how to solve this?

      Thank you
      Hugo
        • 1. Re: Serialize an ArrayList<ArrayList>
          Jonathan.Knight
          When you serialize an ArrayList in POF all Coherence knows on deserialization is that you serialized a Collection and by default gives you back com.tangosol.util.ImmutableArrayList$ListView which implements java.util.List and not java.utilArrayList.

          If there probably is no reason why you specifically need a the inner collection to be an ArrayList then you could do this...
          List<List<Integer>> list = (List<List<Integer>>)reader.readCollection(1, new ArrayList<List<Integer>>());
          There is a related problem since version 3.6 if you serialize other types of Collection (instances of Set or SortedSet). For example if you had a field that was a List of Sets of Integer like this...
          List<Set<Integer>> listOfSets = ....
          and your writeObject method did this...
          pofWriter.writeCollection(100, listOfSets);
          when you call readCollection in the readObject method you will get back List<List<Integer>> not List<Set<Integer>> as Coherence only knows it has seialized a Collection and not what type of Collection it was. This can cause problems particulary if you use things like PofExtractors and other direct POF stream manipulation as you will always extract a List regardless of what type of Collection the field contained.

          JK
          • 2. Re: Serialize an ArrayList<ArrayList>
            833144
            It worked perfectly.

            Thanks
            • 3. Re: Serialize an ArrayList<ArrayList>
              923561
              ahh thank you JK, i was going nuts trying to figure out why it's throwing java.lang.ClassCastException: com.tangosol.util.ImmutableArrayList$ListView cannot be cast to java.util.Set with Map<Short, Set<Short>>

              Is there any fix/workaround that will let me use set instead of switching to Map<Short,List<short>>
              • 4. Re: Serialize an ArrayList<ArrayList>
                robvarga
                920558 wrote:
                ahh thank you JK, i was going nuts trying to figure out why it's throwing java.lang.ClassCastException: com.tangosol.util.ImmutableArrayList$ListView cannot be cast to java.util.Set with Map<Short, Set<Short>>

                Is there any fix/workaround that will let me use set instead of switching to Map<Short,List<short>>
                Yes, if you can afford your Set class to be of a specific implementation type. In that case you can just register that Set implementation class in POF configuration (and of course provide serialization for that). You can even optimize the serialization of the Map this way as it can be written as a fully uniform map.

                Best regards,

                Robert
                • 5. Re: Serialize an ArrayList<ArrayList>
                  alexey.ragozin
                  Hi,

                  You can map standard collection classes to ReflectionPofSerializer in pof-config.
                  Take a look at this thread for more details Re: Can an extend client use both POF and java.io.Serializable?

                  ReflectionPofSerializer (http://code.google.com/p/gridkit/wiki/ReflectionPofSerializer) was tested with many standard collections including TreeMap,LinkedHashMap, unmodificable collections, Arrays.list(...) etc

                  Regards,
                  Alexey