java将list转化成树

项目中需要传递组织机构数据,因为要校验上级站点是否存在(存在才能插入,除非根节点),然后就有了这个代码

 /**
     * 双重for循环方法转换成树形结构
     * @param zCompanyBeanList
     * @return
     */
    public static List<ZCompanyBean> buildTreeCompany(List<ZCompanyBean> zCompanyBeanList) {
        List<ZCompanyBean> result = new LinkedList<>();
        for (ZCompanyBean zCompanyBean : zCompanyBeanList) {
            if (StringUtils.isEmpty(zCompanyBean.getParent_id())) {
                result.add(zCompanyBean);
            }
            for (ZCompanyBean child : zCompanyBeanList) {
                if (child.getParent_id().equals(zCompanyBean.getCode())) {
                    List<ZCompanyBean> children = zCompanyBean.getChildren();
                    if (children == null) {
                        children = new LinkedList<>();
                        zCompanyBean.setChildren(children);
                    }
                    children.add(child);
                }
            }
        }
        return result;
    }

其中实体类如下

@Data
public class ZCompanyBean {
    /**
     * 部门编码
     */
    private String code;
    /**
     * 上级部门
     */
    private String parent_id;
    /**
     * 部门name
     */
    private String name;
    private String remark;
    /**
     * 部门状态0:正常 1:删除  3:停职  4离职   5 退休
     */
    private String is_enable;
    private String oOrgCode;
    /**
     * 层级(用于批量传)
     */
    private int level;

    private List<ZCompanyBean> children;
}

转化成一个树后还需要使用广度遍历,这样可以把一个层级的批量传过去
定义个level属性(层级)

 /**
     * 广度遍历层级(公司)
     * @param zCompanyBeanList
     */
    public static void spanForLevelCompany(List<ZCompanyBean> zCompanyBeanList){
        Queue<ZCompanyBean> queue = new LinkedList<>();
        for (ZCompanyBean zCompanyBean:zCompanyBeanList) {
            int level = 0;
            zCompanyBean.setLevel(level);
            queue.add(zCompanyBean);
            ZCompanyBean tree ;
            tree = queue.poll();
            while (null != tree) {
                if(null!=tree.getChildren()) {
                    for (ZCompanyBean tr : tree.getChildren()) {
                        tr.setLevel(tree.getLevel()+1);
                        queue.offer(tr);
                    }
                }
                tree = queue.poll();
            }
        }
    }

最后是main方法

//构建树
            List<ZCompanyBean> zCompanyTreeList = TreeUtils.buildTreeCompany(zCompanyBeanList);
            //广度遍历层级(公司)
            TreeUtils.spanForLevelCompany(zCompanyTreeList);
            Map<Integer,List<Map<String, Object>>> map = new HashMap<>(10);
            //将传的数据分层级
            getLevelMap(zCompanyTreeList,map);
            for(int i=0;i<map.size();i++){
                List<Map<String, Object>> companyLevelList = map.get(i);
                List<List<Map<String, Object>>> parts = Lists.partition(companyLevelList, maxPostSize);
                for (List<Map<String, Object>> part:parts) {
                	***post传递参数***
                }
            }

附录getLevelMap方法

public void getLevelMap(List<ZCompanyBean> result,Map<Integer,List<Map<String, Object>>> map){
        List<Map<String, Object>> list = null;
        for (ZCompanyBean zCompanyBean:result) {
            Map<String, Object> param = new HashMap<>(10);
            param.put("name", zCompanyBean.getName() + "");
            param.put("department_id", zCompanyBean.getDepartment_id() + "");
            param.put("code", zCompanyBean.getCode() + "");
            String parentId = zCompanyBean.getParent_id() + "";
            param.put("parent_id", parentId);
            param.put("level", zCompanyBean.getLevel());
            param.put("is_enable", zCompanyBean.getIs_enable());
            int level = zCompanyBean.getLevel();
            if(map.containsKey(level)){
                list = map.get(level);
            }else{
                list = new LinkedList<>();
            }
            list.add(param);
            map.put(level,list);
            if(null!=zCompanyBean.getChildren()){
                getLevelMap(zCompanyBean.getChildren(),map);
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
List数据转换成形结构的基本思路是首先遍历List,将其中每个节点的父子关系建立起来,然后从根节点开始递归构建整个形结构。 具体实现步骤如下: 1. 定义节点类,包含节点id、节点名称和子节点列表等属性。 ``` public class TreeNode { private String id; private String name; private List<TreeNode> children; // getter和setter方法 // ... } ``` 2. 遍历List,将每个节点的父子关系建立起来,可以使用Map来存储节点id和对应的节点对象,便于查找父节点。 ``` Map<String, TreeNode> map = new HashMap<>(); for (TreeNode node : list) { map.put(node.getId(), node); String parentId = node.getParentId(); if (parentId != null) { TreeNode parent = map.get(parentId); if (parent != null) { parent.getChildren().add(node); } } } ``` 3. 找到根节点,开始递归构建整个形结构。 ``` public static TreeNode buildTree(List<TreeNode> list) { // 构建Map,方便查找节点 Map<String, TreeNode> map = new HashMap<>(); for (TreeNode node : list) { map.put(node.getId(), node); } // 找到根节点 TreeNode root = null; for (TreeNode node : list) { if (node.getParentId() == null) { root = node; break; } } // 从根节点开始递归构建整个形结构 buildSubTree(root, map); return root; } private static void buildSubTree(TreeNode node, Map<String, TreeNode> map) { List<TreeNode> children = node.getChildren(); if (children == null) { return; } // 遍历子节点,递归构建子 for (TreeNode child : children) { buildSubTree(child, map); } // 根据子节点的顺序重新排序 children.sort(Comparator.comparing(TreeNode::getName)); } ``` 以上就是将List数据转换成行结构的基本实现方法。需要注意的是,这里的代码只是一个简单的示例,实际情况下可能需要根据具体的业务需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值