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