java.util.HashMap.keySet() returns a non-serializable (Abstract) Set

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 6u10
    • Component/s: core-libs

      A DESCRIPTION OF THE REQUEST :
      I'm not sure if this is against the spec, but it's certainly annoying...

      The following code snippet returns HashMap$KeySet, which is a subclass of AbstractSet, and thus not Serializable.The parent HashMap object is Serializable though.

      {
          Set set = map.keySet();
      }

      JUSTIFICATION :
      Main use case for this is if one wants to store a keySet in a J2EE HttpSession - I could store the HashMap, so why can't I store it's keySet()?

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      HashMap$KeySet implements Serializable
      ACTUAL -
      HashMap$KeySet extends AbstractSet
      (and doesn't implement Serializable)

      ---------- BEGIN SOURCE ----------
      See above - no other need for code example
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      new HashSet( map.keySet() );

      Obviously this isn't a good example for performance / efficiency.

            Assignee:
            Michael McMahon
            Reporter:
            Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: