场景:工作中的主数据,每条主数据的等级不一样,但又包含其他等级的属性。找到关系后,输出到Excel 中。
简化模型:
层级树形结构,且每条数据有不同的层级,每个层级取得属性不同,输出为行,可用hutool 导出为Excel
package com.qingyu.controller;
import java.util.*;
/**
* @Author xiaoliutongxue
* @Note Hope we enjoy ourselves everday:)
* @Date 2024-07-31 22:38
**/
public class testDemo {
public static void main(String[] args) {
List<Map<String, Object>> pudongchildren = new ArrayList<>();
Map<String, Object> tangzhen = new HashMap();
tangzhen.put("level", "3");
tangzhen.put("id", "tangzhen");
tangzhen.put("label", "唐镇");
tangzhen.put("tag_1", "");
tangzhen.put("tag_2", "");
tangzhen.put("tag_3", "tag_3唐镇");
tangzhen.put("pudong", "");
Map<String, Object> zhangjinag = new HashMap();
zhangjinag.put("level", "3");
zhangjinag.put("id", "zhangjinag");
zhangjinag.put("label", "张江");
zhangjinag.put("tag_1", "");
zhangjinag.put("tag_2", "");
zhangjinag.put("tag_3", "tag_3_张江");
zhangjinag.put("pudong", "");
pudongchildren.add(tangzhen);
pudongchildren.add(zhangjinag);
Map<String, Object> pudong = new HashMap();
pudong.put("level", "2");
pudong.put("id", "pudong");
pudong.put("label", "浦东新区");
pudong.put("tag_1", "");
pudong.put("tag_2", "tag_2_浦东新区");
pudong.put("tag_3", "");
pudong.put("children", pudongchildren);
List<Map<String, Object>> shanghaigchildren = new ArrayList<>();
shanghaigchildren.add(pudong);
Map<String, Object> shanghai = new HashMap();
shanghai.put("level", "1");
shanghai.put("id", "shanghai");
shanghai.put("tag_1", "tag_1_上海");
shanghai.put("tag_2", "");
shanghai.put("tag_3", "");
shanghai.put("label", "上海");
shanghai.put("children", shanghaigchildren);
List< Map<String, Object >> lists =new ArrayList<>();
Map<String, Object> map= new HashMap<>();
map.put("id", shanghai.get("id"));
map.put("tag_1", shanghai.get("tag_1"));
map.put("level_1_node_id",shanghai.get("id"));
map.put("level_1_node_name",shanghai.get("label"));
// 将数据转化成一行行的数据
List< Map<String, Object >> resultList = treeToList2(shanghai, lists, map);
resultList.forEach(System.out::println);
}
public static List< Map<String, Object >> treeToList2(Map<String, Object> map,
List< Map<String, Object >> list1,
Map<String, Object> map2) {
//遍历递归子节点
List<Map<String, Object>> childrens = (List<Map<String, Object>>) map.get("children");
if (childrens==null||childrens.size() == 0) {
list1.add(map2);
} else {
for (Map<String, Object> children : childrens) {
Map<String, Object> maps = new HashMap<>(map2);
if(children.get("level").equals("2")){
maps.put("level_2_node_id",children.get("id"));
maps.put("level_2_node_name",children.get("label"));
maps.put("tag_2",children.get("tag_2"));
}
if(children.get("level").equals("3")){
maps.put("level_3_node_id",children.get("id"));
maps.put("level_3_node_name",children.get("label"));
maps.put("tag_3",children.get("tag_3"));
}
treeToList2(children, list1, maps);
}
}
return list1;
}
}
输出:
{level_3_node_id=tangzhen, level_1_node_id=shanghai, level_2_node_name=浦东新区, id=shanghai, level_1_node_name=上海, tag_1=tag_1_上海, tag_2=tag_2_浦东新区, level_2_node_id=pudong, tag_3=tag_3唐镇, level_3_node_name=唐镇}
{level_3_node_id=zhangjinag, level_1_node_id=shanghai, level_2_node_name=浦东新区, id=shanghai, level_1_node_name=上海, tag_1=tag_1_上海, tag_2=tag_2_浦东新区, level_2_node_id=pudong, tag_3=tag_3_张江, level_3_node_name=张江}