ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 数据拼装成树结构

2019-04-30 13:47:42  阅读:356  来源: 互联网

标签:return 树结构 import get List 拼装 parentId java public


项目数据需要在后台拼装成树结构,所以写了两种方案来实现:

方案一:数据类型List<XXXVO>

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

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

方案二:数据类型List<Map<String,Object>>

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

基础父类实现如下:

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

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

public class BaseTreeVO<T extends BaseTreeVO> {
    private static final long serialVersionUID = 6753923840108631912L;
    private List<T> children;
    private Long parentId;
    private Long id;

    public BaseTreeVO() {
    }

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

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

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

    public 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 {
            Map groupByParentIdMap = (Map)data.stream().collect(Collectors.groupingBy((item) -> {
                return item.get(key) != null?item.get(key).toString():"nonParent";
            }));
            Map dataMap = (Map)data.stream().collect(Collectors.toMap((item) -> {
                return item.get("id").toString();
            }, (t2) -> {
                return t2;
            }));
            ArrayList resp = new ArrayList();
            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 {
            Map groupByParentIdMap = (Map)data.stream().collect(Collectors.groupingBy((item) -> {
                return item.getParentId() != null?item.getParentId().toString():"nonParent";
            }));
            Map dataMap = (Map)data.stream().collect(Collectors.toMap((item) -> {
                return item.getId().toString();
            }, (t2) -> {
                return t2;
            }));
            ArrayList resp = new ArrayList();
            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;
        }
    }
}

 

标签:return,树结构,import,get,List,拼装,parentId,java,public
来源: https://blog.csdn.net/loveshunyi/article/details/89706039

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有