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

两盒软妹~` 提交于 2019-12-30 05:24:25

问题


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.


回答1:


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);
}



回答2:


In addition to the answers above, I would like to share something I found on the internet too. I have not tested it yet, but it seems to offer an alternative:

http://alexradzin.blogspot.hk/2010/10/hierarchical-structures-with-java-enums_05.html

public enum OsType {
OS(null),
    Windows(OS),
        WindowsNT(Windows),
            WindowsNTWorkstation(WindowsNT),
            WindowsNTServer(WindowsNT),
        Windows2000(Windows),
            Windows2000Server(Windows2000),
            Windows2000Workstation(Windows2000),
        WindowsXp(Windows),
        WindowsVista(Windows),
        Windows7(Windows),
        Windows95(Windows),
        Windows98(Windows),
    Unix(OS) {
            @Override
            public boolean supportsXWindows() {
                return true;
            }
        },
        Linux(Unix),
        AIX(Unix),
        HpUx(Unix),
        SunOs(Unix),
;
private OsType parent = null;

private OsType(OsType parent) {
    this.parent = parent;
}



回答3:


java.util.Map objects with java.util.Collection value types can represent arbitrary tree structures:

    final Map<String,Set<String>> map = Collections.unmodifiableMap(
        new HashMap<String,Set<String>>() {
            {
                put(
                    "A",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("A1", "A2", "A3"))
                    )
                );
                put(
                    "A1",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("A1a", "A1b"))
                    )
                );
                put(
                    "A2",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("A2a", "A2b"))
                    )
                );
                put(
                    "A3",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("A3a", "A3b", "A3c"))
                    )
                );
                put(
                    "B",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("B1", "B2"))
                    )
                );
                put(
                    "B1",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("B1a", "B1b", "B1c"))
                    )
                );
                put(
                    "B2",
                    Collections.unmodifiableSet(
                        new HashSet<>(Arrays.asList("B2a", "B2b"))
                    )
                );
            }
        }
    );

Or you could try something like javax.swing.tree.DefaultTreeModel.



来源:https://stackoverflow.com/questions/20114444/how-to-build-an-hierarchy-tree-of-categories-in-java-using-enums-or-any-other-wa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!