技术就是力量,没有对比就没伤害
六万条数据构建树结构,采用传统构建树方法和.stream()函数构建的对比!!!
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210422203622597.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210422203856511.png)
1、拷贝的递归树逻辑(强烈不推荐)
public static List<CompanyInfo> listToTree(List<CompanyInfo> list) {
//用递归找子。
List<CompanyInfo> treeList = new ArrayList<CompanyInfo>();
for (CompanyInfo tree : list) {
//如果根目录的parentId为-1
if (tree.getParentId() == -1 ) {
treeList.add(findChildren(tree, list));
}
}
return treeList;
}
private static CompanyInfo findChildren(CompanyInfo tree, List<CompanyInfo> list) {
for (CompanyInfo node : list) {
if (node.getParentId().longValue() == tree.getId().longValue()) {
if (tree.getChildren() == null) {
tree.setChildren(new ArrayList<CompanyInfo>());
}
tree.getChildren().add(findChildren(node, list));
}
}
return tree;
}
2、list.stream()构建树(强烈推荐,超快),6万条数据仅仅用了12毫秒
<