递归

        List<ZlCategory> list = zlCategoryService.findAll();
        List<ZlCategory> rootList = new ArrayList<>();
        list.forEach(c -> {
            if (c.getParentId().equals(0L))
                rootList.add(c);
        });

        rootList.forEach(c -> c.setChildList(getChild(c.getId(), list)));

 

    private List<ZlCategory> getChild(Long id, List<ZlCategory> list) {
        List<ZlCategory> childList = new ArrayList<>();
        list.forEach(c -> {
            if (c.getParentId().equals(id))
                childList.add(c);
        });

        //递归
        childList.forEach(c -> c.setChildList(getChild(c.getId(), list)));

        if (childList.size() == 0)
            return new ArrayList<>();
        return childList;
    }

 

posted on 2020-04-13 17:21  1zfang1  阅读(143)  评论(0编辑  收藏  举报

导航