Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4501848

Objects returned by keySet(), entrySet() and values() not Serializable

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.3.1
    • core-libs



      Name: ddT132432 Date: 09/11/2001


      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)


      The methods keySet(), entrySet() and values() return objects that are instances
      of anonymous inner classes. These anonymous inner classes do not implement
      Serializable, so that the Set objects returned by these methods are not
      Serializable. This is inconsistent with the standard JDK collections
      implementation classes, which all implement Serializable.

      This bug leads to unexpected results, see the example program below.

      import java.io.*;
      import java.util.*;

      public class MyTest {

          public static void main(String[] args) throws Exception {

              Map myMap = new HashMap();

              myMap.put(new Integer(1), "one");
              myMap.put(new Integer(2), "two");

              FileOutputStream fos = new FileOutputStream("test.dat");
              ObjectOutputStream oos = new ObjectOutputStream(fos);

              Set obj = myMap.keySet(); // NOTE: This set is NOT Serializable!
              oos.writeObject(obj); // Throws an NotSerializableException

              oos.flush();
              oos.close();

              System.out.println("Success!");
          }
      }
      (Review ID: 131494)
      ======================================================================

            jjb Josh Bloch (Inactive)
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: