• 普通list和树状list互转


    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);
        }
    }
  • 相关阅读:
    Oracle 增加修改删除字段
    asp.net,简单权限。存取读XML
    点击按钮出现60秒禁用倒计时js代码
    vb.net 接口POST方式传参数提交返回值
    给VS2005 2008等添加代码对照线,对齐线
    sql 、linq、lambda 查询语句的区别
    C# asp.net 实现导出Excel
    SQL中使用update inner join和delete inner join
    导入EXCEL表时,提示"找不到可安装的ISAM"怎么办
    ASP.NET后台通过输出JavaScript弹出窗口小结 弹窗弹框
  • 原文地址:https://www.cnblogs.com/zkx4213/p/12910474.html
Copyright © 2020-2023  润新知