0

I have a structure something like this:

                              |----child obj 2
           |---- child obj ---|----child obj 2
Account ---|---- child obj--- |----child obj 2
           |---- child obj ---|----child obj 2
                              |----child obj 2

Diagram isn't the best but.. I want to be able to dynamically update the child objects of the main Account object. There can be n number of child objects. The only way I know to do that now is to store the child objects in a data structure like a hashmap, update what i need to, then put the child object back into the hashmap.

How can I do this more efficiently?

Some example code

public class Account {
    //getters and setters
    ArrayList <ChildObj> listOfChildObjs = new ArrayList();
}

public class childObj {
//getters and setters
ArrayList <ChildObj2> listOfChildObj2s = new ArrayList();
}

public class childObj2 {
//getters and setters
}
dk40149
  • 99
  • 1
  • 10
  • Could you include some Java code that shows the data structure you are currently using? Specifically the definition of `Account`? – Sean Bright Oct 02 '19 at 19:40
  • Please describe your actual task. There is no such thing as *child **object*** in Java. – PM 77-1 Oct 02 '19 at 19:41
  • @Unimportant I added some example code – dk40149 Oct 02 '19 at 19:57
  • @PM77-1 Please see op with edits – dk40149 Oct 02 '19 at 19:57
  • Use a data-appropriate data structure. Specifically: you're showing a tree, so [implement that](https://stackoverflow.com/a/3522481/740553). – Mike 'Pomax' Kamermans Oct 02 '19 at 19:58
  • What are you trying to update exactly? `ChildObj`s or `ChildObj2`s? Both? Are you applying the same change to all of them, or only some? – Ian McLaird Oct 02 '19 at 20:03
  • You have ArrayList of ArrayLists. It's your data structure. Efficiency depends on what you need to do with this data. If you are going to access your objects by their sequential number - then there is nothing wrong with such structure. – PM 77-1 Oct 02 '19 at 21:13

0 Answers0