有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java哈希映射不可序列化

带有Serializable键/值的HashMap应该是Serializable

但这对我不起作用。尝试了其他IO流。都不管用

有什么建议吗

测试代码

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);
    }
}

堆栈跟踪

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

共 (2) 个答案

  1. # 1 楼答案

    代码的工作版本:

    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);
        }
    }
    
  2. # 2 楼答案

    异常消息确切地告诉您问题所在:您正试图序列化类SimpleSerializationTest的实例,而该类不可序列化

    为什么??好的,您已经创建了一个SimpleSerializationTest的匿名内部类,它扩展了HashMap,并且您正在尝试序列化该类的一个实例。内部类始终具有对其外部类的相关实例的引用,默认情况下,序列化将尝试遍历这些实例

    我注意到您使用了双大括号{{ ... }}语法,好像您认为它有某种特殊的意义。重要的是要理解它实际上是两个独立的构造。构造函数调用后立即出现的外部大括号对标记了内部类定义的边界。内部对绑定了一个实例初始值设定项块,例如可以在任何类体中使用(尽管它们在匿名内部类以外的上下文中不常见)。通常,您还将在外部对中包括一个或多个方法实现/重写,在初始值设定项块之前或之后

    请尝试以下方法:

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