【问题标题】:How to build an hierarchy tree of categories in java using enums or any other way?java - 如何使用枚举或任何其他方式在java中构建类别的层次结构树?
【发布时间】:2013-11-22 13:42:45
【问题描述】:

假设我们有一组类别: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中构建层次结构?

由于每个集合的基数是固定的并且预先知道,我最初的方法是使用枚举类型而不是构建具有继承的类,但我愿意接受任何建议。我不知道如何解决这个问题。

提前致谢。

【问题讨论】:

    标签: java enums tree hierarchy categories


    【解决方案1】:

    可能是这样的实现:

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

    【讨论】:

    • +1 - 好主意。我在考虑某种接口层次结构,但这更干净。
    • 由于类别层次结构是预定义的,接口不需要修改器。
    【解决方案2】:

    除了上面的答案,我也想分享我在互联网上找到的一些东西。我还没有测试它,但它似乎提供了一个替代方案:

    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.Collection 值类型的java.util.Map 对象可以表示任意树结构:

          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。
      猜你喜欢
      • 2014-06-01
      • 2011-01-07
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多