java使用stream遍历树结构

@Test
   public void testtree(){
       //模拟从数据库查询出来
       List<Menu> menus = Arrays.asList(
               new Menu(1,"根节点",0),
               new Menu(2,"子节点1",1),
               new Menu(3,"子节点1.1",2),
               new Menu(4,"子节点1.2",2),
               new Menu(5,"根节点1.3",2),
               new Menu(6,"根节点2",1),
               new Menu(7,"根节点2.1",6),
               new Menu(8,"根节点2.2",6),
               new Menu(9,"根节点2.2.1",7),
               new Menu(10,"根节点2.2.2",7),
               new Menu(11,"根节点3",1),
               new Menu(12,"根节点3.1",11)
       );

       //获取父节点
       List<Menu> collect = menus.stream().filter(m -> m.getParentId() == 0).map(
               (m) -> {
                   m.setChildList(getChildrens(m, menus));
                   return m;
               }
       ).collect(Collectors.toList());
       System.out.println("-------转json输出结果-------");
       System.out.println(JSON.toJSON(collect));
   }

   /**
    * 递归查询子节点
    * @param root  根节点
    * @param all   所有节点
    * @return 根节点信息
    */
   private List<Menu> getChildrens(Menu root, List<Menu> all) {
       List<Menu> children = all.stream().filter(m -> {
           return Objects.equals(m.getParentId(), root.getId());
       }).map(
               (m) -> {
                   m.setChildList(getChildrens(m, all));
                   return m;
               }
       ).collect(Collectors.toList());
       return children;
   }




@Data
@Builder
public class Menu {
   /**
    * id
    */
   public Integer id;
   /**
    * 名称
    */
   public String name;
   /**
    * 父id ,根节点为0
    */
   public Integer parentId;
   /**
    * 子节点信息
    */
   public List<Menu> childList;


   public Menu(Integer id, String name, Integer parentId) {
       this.id = id;
       this.name = name;
       this.parentId = parentId;
   }

   public Menu(Integer id, String name, Integer parentId, List<Menu> childList) {
       this.id = id;
       this.name = name;
       this.parentId = parentId;
       this.childList = childList;
   }

}


public List<MenuVO> departmentStructure(String id){
       //step1:这里 map 只是简单转换了返回的对象属性(返回需要的类型),本质还是所有部门数据
       List<MenuVO> menuVOList = this.getMenuListById(id).stream()
               .map(e -> e.copyProperties(Menu.class))
               .collect(Collectors.toList());
       //step2:利用父节点分组,所有部门的父 Id 进行分组,把所有的子节点 List 集合都找出来并一层层分好组
       Map<Integer, List<MenuVO>> menuListMap = menuVOList .stream()
               .collect(Collectors.groupingBy(MenuVO::getParentId));
       //step3:关键一步,关联上子部门,将子部门的 List 集合经过遍历一层层地放置好,最终会得到完整的部门父子关系 List 集合
       menuVOList .forEach(e -> e.setChildrenList(menuListMap.get(e.getId())));
       //step4:过滤出顶级部门,即所有的子部门数据都归属于一个顶级父部门 Id
       List<MenuVO> resultList = menuVOList.stream()
               .filter(e -> Constants.TOP_DEPARTMENT_NUM.equals(e.getParentId()))
               .collect(Collectors.toList());
   return Optional.of(resultList).orElse(null);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值