You need to sign in to do that
Don't have an account?
Multidimensional lists
Hi all,
My first post on the board!
I'm looking into the possibility to print such a hierarchy on a visualforce page:
-Object 1
* Object 3
* Object 4
• Object 5
# Object 7
• Object 6
Objects are all of the same type, and relations between them are also stored in the database.
What I did so far is some looping in order to find objects related to each other.
The problem comes up when I want to add the elements the a List.
My idea was to have a List within a List within a List within a List... and loop over this on my page.
But with this part I'm stuck. How adding them to a list?
Purpose would be to have it structured with a parent object next to a list of child objects:
Object 1, List{
Object 3, null (no list, because no child elements)
Object 4, List {
Object 5, List {
Object 7, null (no list, because no child elements)
}
Object 6, null (no list, because no child elements)
}
}
Is this possible with lists?
Anyone did something similar so far?
Thanks for helping me out!
The code I posted should theoretically be able to handle that type of hiearchy (it shouldn't be infinitely looping, two loops were used, one to build the big list of items with their Id and the second to remap which one belongs where).
All Answers
I would probably use a nested map structure for this type of use case. I'm not sure I completely understand your use-case fully, but here goes:
1) You have a hierarchy of objects
2) Each object is of a different type?
3) Each object *should* have its hierarchical parent_id imprinted upon it
4) You would like to organize a data structure in Apex during runtime to show this hierarchical relationship
You can extend the Map<> structure as deep as you need to, always finishing at your deepest hierarchical object. So, when you want to get your deepest entity, your just chaining through get().get().get().get() to dig through the Map. For objects without children for that particular level, you'll have to put in null placeholders.
To safely traverse the Map, I would build a utility method. Pass the method the level of hierarchy your expected the value your are searching for to be and handle exceptions within this helper method gracefully.
Another way to do this with Map structures but more effectively would be to utilize a Wrapper Class to encapsulate hierarchy placement of each object within a shared structure, and then store collections of those wrappers beneath the hierarchy level.
1) You have a hierarchy of objects
yes
2) Each object is of a different type?
no, all of the same type (Object__C to be precise :-) )
3) Each object *should* have its hierarchical parent_id imprinted upon it
not necessary, I think that with the solution you propose, I'm perfectly able to find the parent, because it is a pair (parent object + map of child objects). Correct?
4) You would like to organize a data structure in Apex during runtime to show this hierarchical relationship
Yes, print out the different levels on a page, somewhat like the example in the first post
Got it! I'm totally following you now.
Yes, given the structure I presented (parent obj + map of children +map of children + map of children +child) you should be able to organize your singular object type quite well. Even without the Parent_Id embedded on each record. As long as you know how they should be organized at runtime.
In fact, what would be the advantage to use a Map for this?
Is is not possible with a List or a Set?
I think you could do this with a custom class combined with a single list object. Something like this may work?
In fact, the amount of levels in the hierarchy will not be fixed upfront. So the looping has to be infinite in order to make sure he goes to the bottom of the structure
The code I posted should theoretically be able to handle that type of hiearchy (it shouldn't be infinitely looping, two loops were used, one to build the big list of items with their Id and the second to remap which one belongs where).