United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6431617 : (coll) AbstractList$Itr is not serializable

Details
Type:
Enhancement
Submit Date:
2006-05-30
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2007-07-02
Component:
core-libs
OS:
linux
Sub-Component:
java.util:collections
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
1.4.2
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.4.2_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_09-b05)
Java HotSpot(TM) Client VM (build 1.4.2_09-b05, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux ******* 2.4.20-46.7.legacysmp #1 SMP Sat Mar 4 14:58:59 EST 2006 i686 unknown

A DESCRIPTION OF THE PROBLEM :
Attempt to serialize an object with an instance of java.util.AbstractList$Itr reachable from it results in a java.io.NotSerializableException

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(new ArrayList().iterator());

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
silently succeed

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.io.NotSerializableException: java.util.AbstractList$Itr

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test
{
    public static void main(String[] args) throws IOException
    {
        new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(new ArrayList().iterator());
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Store the list and an index instead of the iterator. This workaround doesn't work well with sequentially accessible lists, and breaks down completely with nonlist collections though.

                                    

Comments
EVALUATION

See the evaluation of

4782922: ArrayList.subList() does not return a serializable object

Iterators are designed to have a transient existence,
and not to be serializable.  If serializing them was possible,
the entire backing collection would have to be serialized as well.
Non-view Collections have more permanence, and are generally serializable.
                                     
2007-07-02
EVALUATION

Iterators are not intended to be serializable.
Probably.... Not a Defect.
                                     
2006-05-30



Hardware and Software, Engineered to Work Together