HashMap is not Serializable

HashMap with key / value Serializable must be Serializable .

But this does not work for me. Tried some other input / output streams. Nothing works.

Any suggestion?

Test code

 public class SimpleSerializationTest { @Test public void testHashMap() throws Exception { HashMap<String, String> hmap = new HashMap<String, String>() {{ put(new String("key"), new String("value")); }}; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; out = new ObjectOutputStream(bos); out.writeObject(hmap); byte[] yourBytes = bos.toByteArray(); if (out != null) { out.close(); } bos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes); ObjectInput in = null; in = new ObjectInputStream(bis); Object o = in.readObject(); bis.close(); if (in != null) { in.close(); } assertEquals(hmap, o); } } 

Stack trace

 java.io.NotSerializableException: SimpleSerializationTest at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) at SimpleSerializationTest.testHashMap(SimpleSerializationTest.java:18) Process finished with exit code 0 
+6
source share
2 answers

The exception message tells you what the problem is: you are trying to serialize an instance of the SimpleSerializationTest class, and this class is not serializable.

Why? So, you created an anonymous inner class SimpleSerializationTest that extends the HashMap , and you are trying to serialize an instance of this class. Inner classes always have references to the corresponding instance of their outer class, and by default serialization will try to traverse them.

I observe that you use double-binding syntax {{ ... }} , as if you think this has some special meaning. It is important to understand that these are actually two separate structures. The outer pair of curly braces that appears immediately after the constructor is called indicates the boundaries of the definition of the inner class. The inner pair binds the instance initializer block, for example, you can use it in any class (although they are unusual in a context other than anonymous inner classes). Typically, you should also include one or more method implementations / overrides within an external pair, before or after the initializer block.

Try this instead:

  public void testHashMap() throws Exception { HashMap<String, String> hmap = new HashMap<String, String>(); hmap.put(new String("key"), new String("value")); // ... } 
+15
source

The working version of your code is:

 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.HashMap; import org.junit.Test; import junit.framework.Assert; public class SimpleSerializationTest implements Serializable{ @Test public void testHashMap() throws Exception { HashMap<String, String> hmap = new HashMap<String, String>() {{ put(new String("key"), new String("value")); }}; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; out = new ObjectOutputStream(bos); out.writeObject(hmap); byte[] yourBytes = bos.toByteArray(); if (out != null) { out.close(); } bos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes); ObjectInput in = null; in = new ObjectInputStream(bis); HashMap<String, String> o = (HashMap<String, String>) in.readObject(); bis.close(); if (in != null) { in.close(); } Assert.assertEquals(hmap, o); } } 
0
source

All Articles