如何使用枚举或其他方式在Java中构建类别层次结构树?

4
假设我们有一组类别: categories={A,B}。再假设A由子类别{A1,A2,A3}组成,B由子类别{B1,B2}组成。此外,还有更多的子类别,如下所示: 对于A1:{A1a,A1b}, 对于A2:{A2a,A2b}, 对于A3:{A3a,A3b,A3c}, 对于B1:{B1a,B1b,B1c}, 对于B2:{B2a,B2b}。我该如何在Java中构建分层结构?
由于每个集合的基数是固定的并且预先已知,我的初始方法是使用枚举类型而不是构建具有继承关系的类,但我对任何建议都持开放态度。我不知道如何解决这个问题。
提前感谢您。
3个回答

4
可能是这个的实现方式:
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
+1 - 不错的想法。我之前考虑过接口层级的问题,但这个更简洁。 - Will
1
由于类别层次结构是预定义的,因此接口不需要修改器。 - SpaceTrucker

3

java.util.Map 对象中的 java.util.Collection 类型值可以表示任意树形结构:

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

或者你可以尝试使用像javax.swing.tree.DefaultTreeModel这样的东西。

非常感谢您提供的unmodifiableSet想法和DefaultTreeModel。 - Konstantinos Giannakopoulos

3
除了上面的答案,我还想分享一些我在互联网上发现的东西。我还没有测试过它,但它似乎提供了一种替代方法:
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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接