普通list和树状list互转

Posted on 2020-05-18 14:42  zkx4213  阅读(560)  评论(0编辑  收藏  举报
import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    
    private String id;
    private String name;
    private String parentId;
    private String code;
    private List<TreeNode> children;

    public TreeNode(String id, String name, String parentId, String code) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
        this.code = code;
    }

    public void addChildren(TreeNode zone) {
        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(zone);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

}
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;

public class TreeUtils {
    
    /**
     * 普通list转树状list
     * @param treeNodeList
     * @return
     */
    public static List<TreeNode> list2Tree(List<TreeNode> treeNodeList) {
        // Map<String, List<TreeNode>> zoneByParentIdMap = treeNodeList.stream().collect(Collectors.groupingBy(TreeNode::getParentId));
     // treeNodeList.forEach(zone->zone.children = zoneByParentIdMap.get(zone.id));
        // return treeNodeList.stream().filter(v -> v.parentId.equals("0")).collect(Collectors.toList());
        
        Map<String, List<TreeNode>> nodeByParentIdMap = treeNodeList.stream().collect(Collectors.groupingBy(TreeNode::getParentId));
        treeNodeList.forEach(node->node.setChildren(nodeByParentIdMap.get(node.getId())));
        return treeNodeList.stream().filter(v -> v.getParentId().equals("0")).collect(Collectors.toList());
    }
    
    /**
     * 树状list转普通list
     * @param list
     * @return
     */
    public static List<TreeNode> tree2list(List<TreeNode> list) {
        List<TreeNode> result = new ArrayList<>();
        for (TreeNode test : list) {
            List<TreeNode> c = test.getChildren();
            result.add(test);
            if (!CollectionUtils.isEmpty(c)) {
                result.addAll(tree2list(c));
                test.setChildren(null);//
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
        List<TreeNode> nodeList = new ArrayList<>();
        nodeList.add(new TreeNode("10", "福建省","0", "FJS"));
        nodeList.add(new TreeNode("11", "福州","10", "FZ"));
        nodeList.add(new TreeNode("12", "莆田","10", "PT"));
        nodeList.add(new TreeNode("13", "泉州","10", "QZ"));
        nodeList.add(new TreeNode("14", "厦门","10", "XM"));
        nodeList.add(new TreeNode("15", "龙岩","10", "LY"));
        nodeList.add(new TreeNode("20", "浙江省","0", "ZJS"));
        nodeList.add(new TreeNode("21", "杭州","20", "HZ"));
        nodeList.add(new TreeNode("22", "嘉兴","20", "JX"));
        nodeList.add(new TreeNode("23", "宁波","20", "NB"));
        List<TreeNode> treeList = TreeUtils.list2Tree(nodeList);
        List<TreeNode> list = TreeUtils.tree2list(treeList);
        List<TreeNode> treeList1 = TreeUtils.list2Tree(list);
    }
}

 

Copyright © 2024 zkx4213
Powered by .NET 9.0 on Kubernetes