【List<Map<String, Object>>】记录一次树形遍历方法
在我实习过程中,有一次碰到了一个遍历需求,大概是这样的:
我现在拿到了广州这个节点的id,然后我要把整棵树查出来,并且返回方式要和前端匹配,以父子嵌套方式返回,就是这样
然后这个问题折磨了我很久
在同事的帮助下才解决
首先,原对象字段是没有child字段的,是只有parentId进行关联,所以我要先创建一个DTO对象,假如有图示的两个字段要返回(因为真正的数据库表对象中是真的没有child字段的),那么我再加一个child字段
这样:
注意,要自己生成get和set方法
然后开始实现
要明白,根据传进来的id查询全部节点很简单,sql:
SELECTidname parentId
FROM[原表]
WHEREtree_name = ( SELECT tree_name FROM [原表] WHERE id ='xxx' )order by tree_level asc;
大概就是这么个意思
每个人需求不一样。
查出来很简单,但是进行构造有点复杂。
就是要构造一个数据结构。
public List<CustShowDto> showWithAll(String id) {//list是查出来的所有节点,没有父子节点List<Map<String, Object>> list = fmCgCustomInfoMapper.selectWithAll(id);//l是节点的属性集合,就是类似于广州List<CustShowDto> l = new ArrayList<>();//Map<String, List<CustShowDto>> parentMap = new HashMap<>();//遍历集合,把所有的节点的父节点的parentid给赋值for (int i = 0; i < list.size(); i++) {CustShowDto custShowDto = JSONObject.parseObject(JSONObject.toJSONString(list.get(i)), CustShowDto.class);l.add(custShowDto);if (StringUtils.isNotEmpty(custShowDto.getCustTreeParentId())) {if (parentMap.get(custShowDto.getCustTreeParentId()) == null) {parentMap.put(custShowDto.getCustTreeParentId(), new ArrayList<>());}parentMap.get(custShowDto.getCustTreeParentId()).add(custShowDto);}}List<CustShowDto> resList = new ArrayList<>();//把赋值的节点添加到返回集合中for (int i = 0; i < l.size(); i++) {if (StringUtils.isEmpty(l.get(i).getCustTreeParentId())) {resList.add(l.get(i));}if (parentMap.get(l.get(i).getCustId()) != null) {l.get(i).setChildren(parentMap.get(l.get(i).getCustId()));}}return resList;}
主要是构造一个parentMap
,用parentId
作为key
,然后根据parentId
把子节点查出来,这个子节点是parentId
下面挂靠的节点,就像是:
然后把查出来的所有的子节点放入list中,这样就形成了
Map<String, List<CustShowDto>> parentMap = new HashMap<>();
这样大概的数据结构就出来了
然后构建一个
List<CustShowDto> resList = new ArrayList<>();
这个是返回的集合。
集合里面放的啥?Map呀
就是list [map1,map2,map3,...]
意思就是这么个意思,随缘看懂。
记录一下,免得下次又要做这种自己又不会!
全部代码:
public List<CustShowDto> showWithAll(String id) {List<Map<String, Object>> list = fmCgCustomInfoMapper.selectWithAll(id);List<CustShowDto> l = new ArrayList<>();Map<String, List<CustShowDto>> parentMap = new HashMap<>();for (int i = 0; i < list.size(); i++) {CustShowDto custShowDto = JSONObject.parseObject(JSONObject.toJSONString(list.get(i)), CustShowDto.class);l.add(custShowDto);if (StringUtils.isNotEmpty(custShowDto.getCustTreeParentId())) {if (parentMap.get(custShowDto.getCustTreeParentId()) == null) {parentMap.put(custShowDto.getCustTreeParentId(), new ArrayList<>());}parentMap.get(custShowDto.getCustTreeParentId()).add(custShowDto);}}List<CustShowDto> resList = new ArrayList<>();for (int i = 0; i < l.size(); i++) {if (StringUtils.isEmpty(l.get(i).getCustTreeParentId())) {resList.add(l.get(i));}if (parentMap.get(l.get(i).getCustId()) != null) {l.get(i).setChildren(parentMap.get(l.get(i).getCustId()));}}return resList;}
文章仅供参考!
【List<Map<String, Object>>】记录一次树形遍历方法相关推荐
- List(Map(String, Object))转为Fastjson JSONArray
原始的 List<Map<String, Object>> test_list_map 内容如下: [{path=hdfs://manager:9000/testDir/1.t ...
- Map双列集合的用法,遍历方法
Map集合的常用方法. Map集合是一个双列集合,里面的每个元素都是一个键值对. Map<K,V> 有两个泛型, K 表示Map集合中键的类型. V 表示Map集合中值的数据类型. 常用方 ...
- List<Map<String, Object>>——多层嵌套的数据结构
文章目录 快速理解多重嵌套结构 Map基础知识 List 例子 查询数据库 快速理解多重嵌套结构 对于多层嵌套的数据结构,需要层层分解. 以List<Map<String, Object& ...
- Map<String,Object>接收参数,Long类型降级为Integer,报类型转换异常
前言 今天看群里小伙伴问了一个非常有意思的问题: 使用 Map<String,Object> 对象接收前端传递的参数,在后端取参时,因为接口文档中明确该字段类型为 Long ,所以对接收的 ...
- Map<String,Object> map=new HashMap<String,Object>详解
Map是一个接口,即Interface Map<K,V>,其中K-key类型和V-value的类型 它的每个元素包含一个key对象和一个value对象,且在这两个对象之间存在一种映射的对应 ...
- 例解List<Map<String, Object>>存放的对象问题
一.提出问题 代码一: import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java ...
- Map<String,List<Map<String, Object>>>遍历
package com.java.day08; import java.util.*; /** * @Auther: iteration.1 * @Date: 10/4/2021 - 04 - 1 ...
- 关于List<Map<String, Object>>个人理解
首先map<String,Object>是定义了一个Map集合变量,然后list<map<String,Object>>是定义了一个List的集合变量,是map的一 ...
- List<String,Object>针对于集合中Map某个属性值求和
假如你需要List<Map<String,Object>> 中Map中的每个属性值进行累加求和 代码如下: List<Map<String, Object>& ...
最新文章
- AI“暴力美学”背后,预训练模型如何乘风破浪?
- php 常见的算法题,php最常见最经典的算法题(1)
- PHP和ajax详解
- 又一次生产 CPU 高负载排查实践
- C++ 循环链表circular linked list实现算法(附完整源码)
- linux运维面板_phpstudy linux web面板(小皮面板)V0.2版本正式发布
- Jenkins 基础入门
- endpointimpl怎么填参数_App拉新:以老拉新活动怎么做?
- CCF202109-2 非零段划分
- gho镜像安装器linux,Ghost镜像安装器
- android listview 移动到顶部,android listview滚动到顶部
- 合肥工业大学数值分析(计算方法)满分实验代码(python实现)
- 如何在Mac上快速显示桌面以便快速访问
- AlertDialog点击确定必定会dismiss问题
- bzoj3786: 星系探索 //ETT
- PADS的四层板转换成2层板
- Visual Studio Code中设置HTML/HTML5模板
- C语言输入学生成绩算平均值
- python,根据tkinter计数器案例,写了个倒计时器
- OpenGL 镜面反射光