Using a tree to find stuff

Im having trouble figuring out how to use a tree to find stuff, or even if my thought is possible.
Im trying to create a search tree - category, with connection to products.
Something like:
class Category {
    private Integer id;
   private String name;
   private List<Category> children;
}
Now, from this code I should be able to make a visual tree, something like:
-rootCategory
  |-id1, Cat1
  |-id2, Cat2
      |-id3, Cat2_1
      |-id4, Cat2_2
          |-id6, Cat2_2_1
  |-id5, Cat3
Then I have my product:
class Product {
  private Integer id;
  private String name;
  private Category category;
  public Product(String name, Category cat) { &.}
}
say I have:
List<Product> productList;
productList.add(new Product(prod0, Cat2));
productList.add(new Product(prod1,  Cat2_1));
productList.add(new Product(prod2, Cat2_2));
productList.add(new Product(prod3, Cat2_2_1));
productList.add(new Product("prod4", Cat3));

Problem:
I need to figure out a way to return a List<Product> when Cat2 is selected that contains:
Cat2, Cat2_1, Cat2_2, Cat2_2_1.
What is the easiest way to do this?
kristian_grAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sciuriwareCommented:
I guess a map is simpler and quicker.
Think of a HashMap of categories, each being a HashMap of key-child pairs.

;JOOP!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kristian_grAuthor Commented:
Do you have any exaples?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.