Java 层级显示的集合?

Java Collection for hierarchy display?

我正在尝试找到一种方法来使用 table 中的当前数据结构创建资产管理报告:

Company_Name      Company_id      Managing_Company_id

Target             002             150
Coles              003             150
Wesfarmers         150             100
Shell              001             150
Max Company X      100             null
Woolworths Group   250             100
Caltex             201             250
Woolworths         202             250
Aldi               300             null
PGX                650             600   

并且报告会将其迭代到层次结构中

Max Company X
             Wesfarmers
                             Target
                             Coles
                             Shell
             Woolworths Group
                             Caltex
                             Woolworths
Aldi
PGX

是否有允许我管理此结构的集合?

关于使用正确数据结构的问题是主观的。即使以目前的数据结构,获取你需要的信息应该不难。您也可以选择 ListTree。另一方面,您可以轻松地旋转自己的数据结构,例如。自定义 tree 并将每个根节点存储在 HashMap 中。也就是说,Java 不提供存储此类特殊场景的直接方式。如果你有一个大数据集,你应该考虑使用某种基于图形的数据库。

乍一看,我正在考虑 topological sort 用 graph/tree 来处理你的情况,但当我正在实施解决方案时。我认为仅使用 double 映射记录关系没有必要解决您的问题:

  1. idname的关系;
  2. managingmanaged的关系;

然后我们可以有一个解决方案:

public static void main(String args[]) {
    List<List<String>> listList = prepareTestData();
    Map<String, String> idNameMap = new HashMap<>();
    Map<String, String> managingRelationMap = new HashMap<>();
    List<String> theTop = new ArrayList<>();
    for (List<String> list : listList) {
        idNameMap.put(list.get(1), list.get(0));
        if (list.get(2) == null || list.get(2).equals(list.get(1))) { // no managing or managed by itself;
            theTop.add(list.get(1));
        } else {
            managingRelationMap.put(list.get(1), list.get(2));
        }
    }

    // add specials whose managing does not exist;
    for (String s : managingRelationMap.keySet()) {
        if (!idNameMap.containsKey(managingRelationMap.get(s))) {
            theTop.add(s);
        }
    }

    for (String top : theTop) {
        printManagingStructure(top, managingRelationMap, idNameMap, 1);
    }
}

private static List<List<String>> prepareTestData() {
    List<List<String>> listList = new ArrayList<>();
    listList.add(new ArrayList<>(Arrays.asList("Target", "002", "150")));
    listList.add(new ArrayList<>(Arrays.asList("Coles", "003", "150")));
    listList.add(new ArrayList<>(Arrays.asList("Wesfarmers", "150", "100")));
    listList.add(new ArrayList<>(Arrays.asList("Shell", "001", "150")));
    listList.add(new ArrayList<>(Arrays.asList("Max Company X", "100", null)));
    listList.add(new ArrayList<>(Arrays.asList("Woolworths Group", "250", "100")));
    listList.add(new ArrayList<>(Arrays.asList("Caltex", "201", "250")));
    listList.add(new ArrayList<>(Arrays.asList("Woolworths", "202", "250")));
    listList.add(new ArrayList<>(Arrays.asList("Aldi", "300", null)));
    listList.add(new ArrayList<>(Arrays.asList("PGX", "650", "600")));
    return listList;
}

private static void printManagingStructure(String managingId, Map<String, String> managingRelationMap,
                                           Map<String, String> idNameMap, int gapCount) {
    System.out.println(String.format("%" + gapCount * 20 + "s%-20s", "", idNameMap.get(managingId)));
    if (!managingRelationMap.containsValue(managingId)) {
        return;
    }
    managingRelationMap.entrySet().stream().filter(entry -> entry.getValue().equals(managingId))
            .forEach(entry -> {
                printManagingStructure(entry.getKey(), managingRelationMap, idNameMap, gapCount + 1);
            });
}

输出将是:

                Max Company X       
                                    Wesfarmers          
                                                        Shell               
                                                        Target              
                                                        Coles               
                                    Woolworths Group    
                                                        Caltex              
                                                        Woolworths          
                Aldi                
                PGX