gpt4 book ai didi

Java集合用于层次结构显示?

转载 作者:行者123 更新时间:2023-12-02 10:55:02 25 4
gpt4 key购买 nike

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

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

是否有一个集合可以让我管理这个结构?

最佳答案

乍一看,我正在考虑使用图形/树来处理您的情况的拓扑排序,但当我正在实现一个解决方案时。我认为仅使用 double 映射来记录关系来解决您的问题并不是那么必要:

  1. idname之间的关系;
  2. 管理被管理之间的关系;

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

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

关于Java集合用于层次结构显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com