1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public class Test { public static void main(String[] args) {
Module module1 = new Module(1, "M1"); Module module2 = new Module(2, "M2"); Module module3 = new Module(3, "M3"); Module module4 = new Module(4, "M4"); Module module5 = new Module(5, "M5");
Rule r1 = new Rule(11, "R1", 2); Rule r2 = new Rule(12, "R2", 3); Rule r3 = new Rule(13, "R3", 4); Rule r4 = new Rule(14, "R4", 5);
module1.setRules(Arrays.asList(r1, r2)); module2.setRules(Arrays.asList(r3, r4));
List<Module> list = Arrays.asList(module1, module2, module3, module4, module5); List<Integer> ids = list.stream().map(module -> module.getId()).collect(Collectors.toList()); Map<Integer, Module> moduleMap = list.stream().collect(Collectors.toMap(Module::getId, module -> module));
List<Integer> result = new ArrayList<>(); List<Integer> integers = handleTree(module2, ids, moduleMap, result); System.out.println(integers);
}
public static List<Integer> handleTree(Module target, List<Integer> moduleIdList, Map<Integer, Module> moduleMap, List<Integer> result) { if (target != null) { System.out.println("删除模块" + target.getId()); result.add(target.getId()); if (!CollectionUtils.isEmpty(target.getRules())) { for (Rule rule:target.getRules()) { System.out.println("删除连线" + rule.getRuleId()); if (rule.getToId() != 0 && moduleIdList.contains(rule.getToId())) { Module module = moduleMap.get(rule.getToId()); handleTree(module, moduleIdList, moduleMap, result); } } } } return result; } }
|