Serialize HashMap<String, Object> in Java
<p>I am trying to serialise an object of the type <code>HashMap&lt;String, Object&gt;</code>. However, I get <code>NotSerializableException</code> at runtime. Here is the code that I use for serialization</p>

<pre><code>public void WriteModel(String modelFile, HashMap&lt;String, Object&gt; o) {
    try {
        ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (modelFile));
        os.writeObject(o);
        os.close();
    }
    catch (Exception e) {
        System.out.println ("Error in saving model " + modelFile + ": " + e);
        e.printStackTrace();
    }
}
</code></pre>

<p>The HashMap has either an <code>int</code> as value or an object of the following type</p>

<pre><code>public class suggestMenu implements Serializable {
    private static final long serialVersionUID = -8558253517294665710L;
    public List&lt;Integer&gt; suggestions = new ArrayList&lt;Integer&gt;();
    public int freq = 0; 
}
</code></pre>

<p>So basically all of the objects contained in HashMap are serializable.
What is it that I am doing wrong? Has it something to do with the fact that HashMap contains more than one type?
And what could be the possible way of serializing it, given that it is almost impossible to change its structure?</p>

<p>Here is the full stacktrace of the error</p>

<pre><code>Error in saving model /home/ritesh/Documents/spell-checker/dict_ser.ser: java.io.NotSerializableException: spell.SpellCheckTrainer
java.io.NotSerializableException: spell.SpellCheckTrainer
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at java.util.HashMap.writeObject(HashMap.java:1129)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at spell.SpellCheckTrainer.WriteModel(SpellCheckTrainer.java:254)
at spell.SpellCheckTrainer.CreateDictionary(SpellCheckTrainer.java:246)
at spell.SpellCheckTrainer.main(SpellCheckTrainer.java:55)
</code></pre>