Losing an object reference after deserialization

Suppose I have two HashMaps: hashMapFooand hashMapBar.

I create some kind of object objCakeand putit on both cards, so each card has a link to objCake, and when I make some changes objCakedepending on which card I get from it, I get the correct state of my object.

After I serialize both cards and deserialize them, I run into the problem that my object has objCakebecome two different objects! I change his state to hashMapFoo, but nothing happens to hashMapBar. hashMapBarno longer contains the correct link! All maps and objects implement Serializable.

Can someone explain?

+5
source share
3

:

public class MapSerializationTest {
    private static class Foo implements Serializable {
    }

    public static void main(String[] args) throws Exception {
        Foo foo = new Foo();

        Map<String, Foo> map1 = new HashMap<String, Foo>();
        map1.put("foo", foo);
        Map<String, Foo> map2 = new HashMap<String, Foo>();
        map2.put("foo", foo);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(map1);
        oos.writeObject(map2);
        oos.close();

        byte[] bytes = baos.toByteArray();

        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        map1 = (Map<String, Foo>) ois.readObject();
        map2 = (Map<String, Foo>) ois.readObject();
        System.out.println(map1.get("foo") == map2.get("foo")); // prints true
    }
}

. , reset() ObjectOutputStream . ObjectOutputStream.

+6

HashMaps, , , , Java , . , , ?

public class Container implements Serializable {
   private Map<Object, Object> hashMapFoo ;
   private Map<Object, Object> hashMapBar;

  //...
}

, , , ObjectInputStream ObjectOutputStream / .

:

:

public static void test() {

    class Container implements Serializable {
            Map<String,StringBuilder> map1 = new HashMap<String, StringBuilder>();
            Map<String,StringBuilder> map2 = new HashMap<String, StringBuilder>();
    }

    try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("jedis.bin"))){

        StringBuilder text = new StringBuilder("Hello Elvis");

        Container container = new Container();
        //same object in two different maps
        container.map1.put("one", text);
        container.map2.put("one", text);

        out.writeObject(container);

    }catch(IOException e) {
        System.out.println(e.getMessage());
    }

    try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("jedis.bin"))) {
        Container container = (Container) in.readObject();
        StringBuilder text1 = container.map1.get("one");
        StringBuilder text2 = container.map2.get("one");

        assert text1 == text2 : "text1 and tex2 are not the same reference";

    }catch(Exception e) {
        System.out.println(e.getMessage());
    }
}
+4

: :

A serialization package cannot be used to recreate or reinitialize objects. Deamination of the byte stream may lead to the creation of new objects , but will not overwrite or modify the contents of existing objects.

Also - having an external link and changing the state of an object using this (instead of getting the link from the map) is a bad idea.

+2
source

All Articles