Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build an hierarchy tree of categories in java using enums or any other way?

Assuming that we have a set of categories: categories={A,B}. Let's assume more that A consists of subcategories: {A1,A2,A3} and B consists of subcategories: {B1,B2}.In addition, there are even more subcategories as follows: for A1:{A1a,A1b}, for A2:{A2a,A2b}, for A3:{A3a,A3b,A3c}, for B1:{B1a,B1b,B1c}, for B2:{B2a,B2b}. How can I build an hierarchical structure in java?

Since the cardinality of each set is fixed and known in advance, my initial approach is to use enum types instead of building classes with inheritance, but I am open to any suggestion. I do not know how to approach this problem.

Thanks in advance.

like image 712
Konstantinos Giannakopoulos Avatar asked Oct 25 '25 05:10

Konstantinos Giannakopoulos


1 Answers

Probably an implementation of this:

public interface Category {
    String getName();
    Category getParent();
    List<Category> getSiblings();
    List<Category> getChildren();
    List<Category> getDescendants();
    void addChild(Category category);
    void addChildren(List<Category> categories);
}
like image 75
Michael Avatar answered Oct 26 '25 18:10

Michael