public List<MonitorPlanComponentTree> findAll() {
Comparator<MonitorPlanComponentTree> comparator = Comparator.comparing(MonitorPlanComponent::getSort,StrUtil::compareVersion);
List<MonitorPlanComponentTree> trees = new ArrayList<>();
List<MonitorPlanComponentTree> all = componentMapper.findAll();
Table<String, String, MonitorPlanComponentTree> data = all
.stream()
.map(item -> ImmutableTable.of(item.getParentId(), item.getId(), item))
.collect(HashBasedTable::create, HashBasedTable::putAll, HashBasedTable::putAll);
all.forEach(e -> {
if ("-1".equals(e.getParentId())) {
trees.add(e);
}
if (data.containsRow(e.getId())) {
Map<String, MonitorPlanComponentTree> row = data.row(e.getId());
e.getChildrens().addAll(row.values());
e.getChildrens().sort(comparator);
}
});
trees.sort(comparator);
return trees;
}
注*:
1.Table 为guava中的数据结构,本人只是为了方便排序, 不考虑排序的话 正常在循环里找父亲然后放在父亲的孩子集合里即可
2.这里默认认为头节点的parentId 为 "-1" 酌情修改