简体   繁体   English

如何反序列化对象的哈希图?

[英]How to deserialize hashmap of objects?

I have a hashmap of objects in java. 我在Java中有对象的哈希表。 I want to deserialize the objects but since the collection is dynamic, I'm confused on how I would read the objects back in (the number of objects is not known when deserializing). 我想反序列化对象,但是由于集合是动态的,所以我对如何读回对象感到困惑(反序列化时未知对象的数量)。 I tried to look around online but couldn't find much. 我试图在网上四处寻找,但找不到很多。

HashMap patron = new HashMap();
HashMap book = new HashMap();
public static void main(String args[]){

}
public void refresh(){
    try {
        FileInputStream fis = new FileInputStream("Books.ser");
        ObjectInputStream os = new ObjectInputStream(fis);
        Set set = book.entrySet();
        Iterator i = set.iterator();
        while(i.hasNext()){

        }
        os.close();

    }
}

I don't know how to complete the while loop where the deserialization takes place. 我不知道如何完成反序列化发生的while循环。

  1. If you serialized the HashMap as a single object, just deserialize it the same way: 如果将HashMap序列化为单个对象,则可以使用相同的方法反序列化它:

     HashMap hm = (HashMap)ois.readObject(); 
  2. If you didn't serialize it as a single object, change it so you do. 如果您未将其序列化为单个对象,请进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM