title: How to read and write a HashMap to a file?
body: <p>
  I have the following <strong>HashMap</strong>:     </p>
  
  <pre><code>HashMap&lt;String,Object&gt; fileObj = new HashMap&lt;String,Object&gt;();
  
  ArrayList&lt;String&gt; cols = new ArrayList&lt;String&gt;();  
  cols.add("a");  
  cols.add("b");  
  cols.add("c");  
  fileObj.put("mylist",cols);  
  </code></pre>
  
  <p>I write it to a file as follows:</p>
  
  <pre><code>File file = new File("temp");  
  FileOutputStream f = new FileOutputStream(file);  
  ObjectOutputStream s = new ObjectOutputStream(f);          
  s.writeObject(fileObj);
  s.flush();
  </code></pre>
  
  <p>Now I want to read this file back to a HashMap where the Object is an ArrayList.
  If i simply do:</p>
  
  <pre><code>File file = new File("temp");  
  FileInputStream f = new FileInputStream(file);  
  ObjectInputStream s = new ObjectInputStream(f);  
  fileObj = (HashMap&lt;String,Object&gt;)s.readObject();         
  s.close();
  </code></pre>
  
  <p>This does not give me the object in the format that I saved it in.
  It returns a table with 15 null elements and the &lt; mylist,[a,b,c] > pair at the 3rd element. I want it to return only one element with the values I had provided to it in the first place.<br>
      //How can I read the same object back into a HashMap ?</p>
  
  <p>OK So based on Cem's note: This is what seems to be the correct explanation:</p>
  
  <p><strong>ObjectOutputStream serializes the objects (HashMap in this case) in whatever format that ObjectInputStream will understand to deserialize and does so generically for any Serializable object. 
  If you want it to serialize in the format that you desire you should write your own serializer/deserializer.</strong></p>
  
  <p><em>In my case: I simply iterate through each of those elements in the HashMap when I read the Object back from the file and get the data and do whatever I want with it. (it enters the loop only at the point where there is data).</em></p>
  
  <p>Thanks,
  </p>