HashMap不可序列化 [英] HashMap not Serializable

查看:650
本文介绍了HashMap不可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有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
 

解决方案

该异常消息告诉您确切的问题是:您正在尝试序列化类SimpleSerializationTest的实例,并且该类不可序列化.

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

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

尝试以下方法:

    public void testHashMap() throws Exception {
        HashMap<String, String> hmap = new HashMap<String, String>();

        hmap.put(new String("key"), new String("value"));

        // ...
    }

HashMap with Serializable key/value is supposed to be Serializable.

But it's not working for me. Tried some other IO streams. None 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

解决方案

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

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

I observe that you use a double-brace {{ ... }} syntax as if you think it has some sort of special significance. It is important to understand that it is actually two separate constructs. The outer pair of braces appearing immediately after a constructor invocation mark the boundaries of the inner class definition. The inner pair bound an instance initializer block, such as you can use in any class body (though they are unusual in contexts other than anonymous inner classes). Ordinarily, you would also include one or more method implementations / overrides inside the outer pair, either 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"));

        // ...
    }

这篇关于HashMap不可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆