今天写了一个通用树构建方法,非常通用和简单,分享出来

基础类
@Data
public class Tree<T> { String id; String pId; List<T> children; } 测试类 @Data public class TreeDemo extends Tree<TreeDemo> { String name; } 工具类 public class TreeUtils { public static List buildTree(List<Tree> trees, String pId) { for (Tree tree : trees) { for (Tree tree1 : trees) { if (tree1.getPId().equals(tree.getId())) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList()); } tree.getChildren().add(tree1); } } } return trees.stream().filter(tree -> tree.getPId().equals(pId)).collect(Collectors.toList()); } }
测试方法

public static void main(String[] args) {
    System.out.println(treeDemo);
}
 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-09-22
  • 2021-09-28
猜你喜欢
  • 2022-03-06
  • 2021-05-10
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-11-17
  • 2022-12-23
相关资源
相似解决方案