My menu hierarchy looks like this:
Root
ProductGroups
Products
ItemTypes
Items
I avoided circular references so there are only references from a parent to its childs, e.g. a ProductGroup references its Products.
If I want to know to which Product an Item belongs to I have to iterate through the hierarchy starting from the Root. This results in 5 for-loops:
for (MenuElement rootChild : Root.getChildren())
for(ProductGroup group : rootChild.getChildren())
for(Product product: group.getChildren())
for(ItemType itemType : product.getChildren())
for(Item item : itemType.getChildren())
if(item == searchedItem)
return product;
Is this the best method or do you think that a recursive approach is better?
There shouldn't be a problem with performance: 10 ProductGroups, 10 Products, 6 ItemTypes, ~30 Items, so recursive approach wouldn't lead to a big overload.
One argument for recursion would be the shorter code. Are there more in this specific case?