0

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.

12
  • You would need to serialize both BSTs and the PQ to the same ObjectOutputStream, and deserialize ditto from the same ObjectInputStream. Object identity is preserved within a single object stream, but it can't be preserved across different streams, Commented May 1, 2024 at 1:28
  • To clarify, I serialized all of the data structures; I just added one of them to avoid repetition. But I'm not sure about the "same" ObjectInputStream part. How can you create a single objectInputStrem for multiple functions? Commented May 1, 2024 at 1:30
  • Create one ObjectOutputStream and call writeObject() three times. Create one ObjectInputStream and call readObject() three times. Commented May 1, 2024 at 1:31
  • 1
    I've told you how to do it. Commented May 1, 2024 at 1:36
  • 1
    Thank you a lot for your help. My program works right now, thanks. Commented May 1, 2024 at 23:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.