java 数据拼装成树结构

7 篇文章 0 订阅

项目数据需要在后台拼装成树结构,所以写了

两种方案来实现:

方案一:要处理的数据类型为List<XXXVO>

1>、首先增加树结构数据基础父类BaseTreeVO,然后需要拼装树结构的VO类需要继承这个基础父类。

2>、创建并调用数据拼装数工具类

方案二:要处理的数据类型为List<Map<String,Object>>

调用工具类,传入标记父Id的字段

 

两种方案实现思路:

1、遍历初始数据集合,将数据按照父Id进行分组,相同父Id的数据放入同一个集合中,并最终生成以父Id为key,相同父Id的数据集合为值的Map数据集,Map<parentId, List<node>>

2、遍历初始数据集合,将数据的Id作为key,数据本身作为值,创建Map数据集。Map<id, node>

3、遍历Map<parentId, List<node>>,根据键值parentId在 数据集Map<id, node>查找对应父节点,如果父节点存在,则对应将子节点集合为值赋给父元素children属性;否则将parentId对应节点集合放入最终结果集中List<Node>。(为毛这样了? 父节点存在,那么这些节点在最终结果树上是子节点,若父节点不存在,那么说明在本次查询结果中,这些节点在页面树上是根节点,最终我们只用把根节点放入结果集合中就好)

4、遍历完成,List<Node>就是我们已经拼装好的树。

 

具体代码实现:

基础父类实现如下:

package com.xxxx.icop.framework.skeleton.template;

import com.xxxx.icop.framework.skeleton.template.BaseVO;
import java.util.List;

public class BaseTreeVO implements Serializable {
    private static final long serialVersionUID = 6753923840108631912L;
    private List children;
    private Long parentId;
    private Long id;

    public BaseTreeVO() {
    }

    public Long getParentId() {
        return this.parentId;
    }

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

    public <T extends BaseTreeVO> List<T> getChildren() {
        return this.children;
    }

    public <T extends BaseTreeVO> void setChildren(List<T> children) {
        this.children = children;
    }

    public String toString() {
        return super.toString();
    }

    public Long getId() {
        return this.id;
    }

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

数据拼装树结构工具类如下:

package com.xxxx.icop.framework.skeleton.template;

import com.xxxx.icop.framework.skeleton.template.BaseTreeVO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;

public class BaseTreeHelper {
    private static final String KEY_CHILDREN = "children";

    public BaseTreeHelper() {
    }

    public static List<Map<String, Object>> createTree(List<Map<String, Object>> data, String key) {
        if(CollectionUtils.isEmpty(data)) {
            return new ArrayList();
        } else {
            //将集合中所有数据按照父Id进行分组,放入Map中,Map<parntId, List<Child>> 
            Map groupByParentIdMap = (Map)data.stream().collect(Collectors.groupingBy((item) -> {
                return item.get(key) != null?item.get(key).toString():"nonParent";
            }));
            //将集合中所有数据以数据Id为key,放入Map中,Map<id, Map<String,Object>>
            Map dataMap = (Map)data.stream().collect(Collectors.toMap((item) -> {
                return item.get("id").toString();
            }, (t2) -> {
                return t2;
            }));
            ArrayList resp = new ArrayList();
            //遍历数据,将子节点放入对应父节点Children属性中
            groupByParentIdMap.keySet().forEach((parentId) -> {
                if(dataMap.containsKey(parentId)) {
                    Object child = (List)((Map)dataMap.get(parentId)).get("children");
                    if(CollectionUtils.isEmpty((Collection)child)) {
                        child = new ArrayList();
                        ((Map)dataMap.get(parentId)).put("children", child);
                    }

                    ((List)child).addAll((Collection)groupByParentIdMap.get(parentId));
                } else {
                    resp.addAll((Collection)groupByParentIdMap.get(parentId));
                }

            });
            return resp;
        }
    }

    public static <T extends BaseTreeVO> List<T> createTree(List<T> data) {
        if(CollectionUtils.isEmpty(data)) {
            return new ArrayList();
        } else {
            //将集合中所有数据按照父Id进行分组,放入Map中,Map<parntId, List<Child>> 
            Map groupByParentIdMap = (Map)data.stream().collect(Collectors.groupingBy((item) -> {
                return item.getParentId() != null?item.getParentId().toString():"nonParent";
            }));
            //将集合中所有数据以数据Id为key,放入Map中,Map<id,T>
            Map dataMap = (Map)data.stream().collect(Collectors.toMap((item) -> {
                return item.getId().toString();
            }, (t2) -> {
                return t2;
            }));
            ArrayList resp = new ArrayList();
            //遍历数据,将子节点放入对应父节点Children属性中
            groupByParentIdMap.keySet().forEach((parentId) -> {
                if(dataMap.containsKey(parentId)) {
                    Object child = ((BaseTreeVO)dataMap.get(parentId)).getChildren();
                    if(CollectionUtils.isEmpty((Collection)child)) {
                        child = new ArrayList();
                    }

                    ((List)child).addAll((Collection)groupByParentIdMap.get(parentId));
                    ((BaseTreeVO)dataMap.get(parentId)).setChildren((List)child);
                } else {
                    resp.addAll((Collection)groupByParentIdMap.get(parentId));
                }

            });
            return resp;
        }
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值