I'm writing a large scale program in java where I keep track of the Nodes with different data structures.
Specifically, one of my Node type have 2 left & right pointers for 2 different Binary Search Trees and I also keep track of that Node in a priority queue.
I have to serialize and deserialize my program to be able to use it for multiple times despite shutdown.
The problem is, when I insert a Node in my program and check the same node in the 3 different structures (2 BSTs and 1 Heap) it's the same exact Node as it should be (I'm using the pointers in the Node to construct BST and it's a part of the heap array).
But whenever I restart my program (serialize / deserilaize) and check the "supposed to be same" Node in the structures, I see 3 duplicates rather than 1 node with the same memory location. My code is definitely creating duplicates while the save/load process.
Here's an outline of my loading (deserilaize) function:
public BSTStructure load() {
BSTStructure bstStructure = null;
try {
File file = new File("filepath");
boolean fileCreated = file.createNewFile();
FileInputStream fileInput = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileInput);
bstStructure = (BSTStructure) in.readObject();
fileInput.close();
in.close();
return bstStructure;
} catch (FileNotFoundException e) {
System.out.println("filepath file is not found");
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
} catch (EOFException e) {
System.out.println("file is Empty");
bstStructure = new BSTSTructre();
} catch (Exception e) {
e.printStackTrace();
}
return moviesByID;
}
public void save(BSTStructre bstStructure) {
try {
FileOutputStream file = new FileOutputStream("filepath");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(bstStructure);
out.close();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
My save/load functions for the other data structures are similar.
I hope that you can help me with this.
I narrowed diwon my problem from a +2000 lines of code to this specific issue.
I can't reproduce the issue unless I restart (save / load) the program.
ObjectOutputStream, and deserialize ditto from the sameObjectInputStream. Object identity is preserved within a single object stream, but it can't be preserved across different streams,ObjectOutputStreamand callwriteObject()three times. Create oneObjectInputStreamand callreadObject()three times.