Why doesn't java.util.List implement Serializable?
Solution 1
List
does not implement Serializable
because is it not a key requirement for a list. There is no guarantee (or need) that every possible implementation of a List
can be serialized.
LinkedList
and ArrayList
choose to do so, but that is specific to their implementation. Other List
implementations may not be Serializable
.
Solution 2
List is an interface and making it extend Serializable would mean that any implementation of List should be serializable.
The serializable property is not part of the List abstraction and should therefore not be required for an implementation.
Solution 3
No. A LinkedList is always a List. When you deserialize the linked list, since a LinkedList is a List, you may write
List l = (List) objectInputStream.readObject();
The fact that l is in fact a LinkedList is not important. You wanted a List, and you got a List.
Solution 4
Because List
is open to be implemented by user specific subclasses as well, and implementors may not necessarily want to implement Serializable
. Serializability does not belong to the key responsibilities of a List
either, so there is no reason to link the two together.
Solution 5
Consider hypothetical ThreadList implements List<Thread>
, containing the list of active threads at any given point in time. The implementation transparently browses active threads and allows easy access to them - for your convenience. Should such an implementation be serializable (forgetting that Thread
is not serializable)?
It is up to the person implementing the interface to decide, whether her implementation is safe to be serialized. List
is too generic, as basically stating *ordered collection of items of type T`.
Comments
-
Swaranga Sarma over 4 years
Why is it that
java.util.List
does not implementSerializable
while subclasses likeLinkedList
,Arraylist
do? Does not it seem to be against inheritance principles? For example if we want to send a Linkedlist over a network, we have to write:new ObjectOutputStream(some inputStream).writeObject(some LinkedList);
So far so good, but while reading the object on the other side we have to explicity say
LinkedList l = (LinkedList)objectInputStream.readObject();
instead ofList l = (List)objectInputStream.readObject();
. If we were ever to change the writing functionality fromLinkedList
to sayArrayList
, we will also have to change the reading part. HavingList
implementSerializable
would have solved the problem. -
Swaranga Sarma over 13 yearsIn that case the same argument can be applied to LinkedList as well that being a very basic list, it does not to be serializable as it is not a requirement of a list.
-
JB Nizet over 13 yearsThere is a difference in forcing all the implementations of a list to be serializable, and choosing that one particular implementation is serializable.
-
Dunaril over 13 yearsLinkedList is an implementation, to which developers choose to give the serializable property. If we follow your reasoning no class should implement two interfaces at a time...
-
davin over 13 yearspresumably the OP meant that
List
should extendSerializable
in addition toCollection
, the fact that he used the word implement should be understood in context... -
Dunaril over 13 yearsAre you sure ? How would the runtime understand how to deserialize the object if you cast it to a non serializable interface ?
-
Puce over 13 yearsThe cast happens after the deserialization and is thus not important here -> cast to List is fine
-
JB Nizet about 13 yearsThe runtime knows how to seserialize the object because its serialized byte stream contains the name of the class of the object that has been serialized. ObjectInputStream thus creates an instance of this class and populates it with the data found in the serialized object, and then returns it to the caller. The caller knows that it was a list that was serialized, and thus casts the returned object into a List.