Sublist not Serializable

I guess this was already known by some, but I didn’t know that if you sublist a List that was Serializable, you will get back a list that is not.


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SerializableSubListTest {
   public static void main(String[] args) {
     List list = new ArrayList();
     list.add(new Serializable(){private static final long serialVersionUID = 1L;});

     System.out.println("list instanceof Serializable = " + (list instanceof Serializable));

     List subList = list.subList(0, 1);

System.out.println("subList instanceof Serializable = " + (subList instanceof Serializable));
   }
}

list instanceof Serializable = true
subList instanceof Serializable = false

Leave a Reply

Your email address will not be published. Required fields are marked *