JDK-6198541 : (coll) Should Hashtable collection views be Serializable?
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.3.1,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux,windows_2000
  • CPU: x86
  • Submitted: 2004-11-22
  • Updated: 2012-10-08
  • Resolved: 2007-08-09
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux tsang 2.6.8-1.521 #1 Mon Aug 16 09:01:18 EDT 2004 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
(Hashtable.values() instanceof Serializable) returns true, but Hashtable.values() is not Serialiable

(Hashtable.keySet() instanceof Serializable) returns true, but Hashtable.keySet() is not Serialiable

(Hashtable.entrySet() instanceof Serializable) returns true, but Hashtable.entrySet() is not Serialiable

And the javadoc does not metioned that they are not Serializable

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
(Hashtable.values() instanceof Serializable) should returns false

(Hashtable.keySet() instanceof Serializable) should returns false

(Hashtable.entrySet() instanceof Serializable) should returns false

the javadoc should metion that keys(), values(), keySet(), and entrySet() from Hashtable is not Serializable

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Hashtable;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Set;
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class TestSerializable {

   public static void main(String[] args) throws java.io.IOException {
      Hashtable h = new Hashtable();
      h.put("a", "aaaaa");

      Collection c = h.values();
      if (c instanceof Serializable)
         System.out.println("... h.values(), yes");
      else
         System.out.println("... h.values(), no");
      try {
         FileOutputStream fos = new FileOutputStream("/tmp/o.tmp");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(c);
         oos.close();
      } catch (Exception e) {
         System.out.println("...... h.values()");
         e.printStackTrace();
      }

      Enumeration en = h.keys();
      if (en instanceof Serializable)
         System.out.println("... h.keys(), yes");
      else
         System.out.println("... h.keys(), no");
      try {
         FileOutputStream fos = new FileOutputStream("/tmp/o.tmp");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(en);
         oos.close();
      } catch (Exception e) {
         System.out.println("...... h.keys()");
         e.printStackTrace();
      }

      Set s = h.keySet();
      if (s instanceof Serializable)
         System.out.println("... h.keySet(), yes");
      else
         System.out.println("... h.keySet(), no");
      try {
         FileOutputStream fos = new FileOutputStream("/tmp/o.tmp");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(s);
         oos.close();
      } catch (Exception e) {
         System.out.println("...... h.keySet()");
         e.printStackTrace();
      }

      s = h.entrySet();
      if (s instanceof Serializable)
         System.out.println("... h.entrySet(), yes");
      else
         System.out.println("... h.entrySet(), no");
      try {
         FileOutputStream fos = new FileOutputStream("/tmp/o.tmp");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(s);
         oos.close();
      } catch (Exception e) {
         System.out.println("...... h.entrySet()");
         e.printStackTrace();
      }
   }

}

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

Comments
EVALUATION Serializing collection views has very strange semantics. The underlying collection would likely have to be serialized as well, but would then be inaccessible when de-serialized? This sounds rather error-prone. Sublists tend to work the same way as Hashtable views. Closing as Not a Defect.
09-08-2007

EVALUATION ###@###.### 2004-11-22 22:06:57 GMT
22-11-2004