根据资源菜单Model,构建 树型结构



import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wondertek.pis.bean.vo.ResourceTreeVO;

import java.util.List;
import java.util.Map;

/**
 * 根据资源菜单Model,构建 tree 型结构
 */
public class ResourceTreeUtil {

    private List<ResourceTreeVO> rootList; //根节点对象存放到这里
    private List<ResourceTreeVO> bodyList; //其他节点存放到这里

    public ResourceTreeUtil(List<ResourceTreeVO> rootList, List<ResourceTreeVO> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    // 构建Tree方法调用
    public List<ResourceTreeVO> getTree() {
        if (bodyList != null && !bodyList.isEmpty()) {
            // 声明 map,用来过滤已操作过的数据
            Map<Long, Long> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree, map));
            return rootList;
        }
        return null;
    }

    // 递归实现获取子集
    public void getChild(ResourceTreeVO beanTree, Map<Long, Long> map) {
        List<ResourceTreeVO> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getResourceId()))
                .filter(c -> c.getParentId().equals(beanTree.getResourceId()))
                .forEach(c -> {
                    map.put(c.getResourceId(), c.getParentId());
                    getChild(c, map);
                    childList.add(c);
                });
        beanTree.setChildren(childList);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值