Java实现List集合转树形结构的示例详解

作者:Zz要变强 时间:2021-11-11 10:48:33 

在开发中,我们通常需要将从数据库中查询的集合数据转换成类似文件系统一样的树形集合,比如:省市单位,部门机构,书籍分类等

TreeNode对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TreeNode {
   /**
    * 节点内码
    */
   private Long id;
   /**
    * 节点名称
    */
   private String label;
   /**
    * 父节点内码
    */
   private Long pid;
   /**
    * 子节点集合
    */
   private List<TreeNode> children;
}

转换工具类

方式一:foreach遍历

对所传递的treeList进行遍历,然后判断该节点的父id与传递的id是否相同,相同则递归设置其孩子节点,并将该节点的放入children集合中,用于统一返回父节点相同的所有TreeNode对象。

方式二:stream流

基于filter()实现数据过滤

该方法会接收一个返回boolean的函数作为参数,终返回一个包括所有符合条件元素的流。

基于map()对元素进行转换

它接收一个函数作为方法参数,这个函数会被应用到集合中每一个 元素上,并终将其映射为一个新的元素。

对集合中的元素进行过滤,通过filter将父id相同的所有节点过滤出来,然后在map中递归设置其孩子节点,并返回。

public class List2TreeList {

//方式一:  使用foreach转换
   public static List<TreeNode> buildTreeUseList(List<TreeNode> treeList,long id){
       //收集传递的集合中父id相同的TreeNode
       List<TreeNode> children = new ArrayList<>();
       for (TreeNode treeNode : treeList) {
           //判断该节点的父id,是否与传入的父id相同,相同则递归设置其孩子节点,并将该节点放入children集合中
           if(treeNode.getPid() == id){
               //递归设置其孩子节点
               treeNode.setChildren(buildTreeUseList(treeList, treeNode.getId()));
               //放入children集合
               children.add(treeNode);
           }
       }
       return children;
   }

//方式二:  使用stream流转换
   public static List<TreeNode> buildTreeUseStream(List<TreeNode> treeList,long id){
       List<TreeNode> list = treeList.stream()
               //过滤父节点与传递的id相同的TreeNode对象
               .filter( treeNode -> treeNode.getPid().longValue() == id )
               .map( treeNode -> {
                   //递归设置孩子节点
                   treeNode.setChildren(buildTreeUseStream(treeList,treeNode.getId()));
                   return treeNode;
               })
               .collect(Collectors.toList());
       return list;
   }
}

功能测试

传入集合数据及最高节点的父id进行转换

@RestController
public class TestController {

@GetMapping("/treeList")
   public List<TreeNode> convert2Tree(){
       List<TreeNode> list = new ArrayList<>();
       list.add(new TreeNode(370000l,"山东省",0l,null));
       list.add(new TreeNode(370100l,"济南市",370000l,null));
       list.add(new TreeNode(370200l,"青岛市",370000l,null));
       list.add(new TreeNode(370300l,"淄博市",370000l,null));
       list.add(new TreeNode(371300l,"临沂市",370000l,null));
       list.add(new TreeNode(370102l,"历下区",370100l,null));
       list.add(new TreeNode(370103l,"市中区",370100l,null));
       list.add(new TreeNode(370202l,"市南区",370200l,null));
       //使用list转换
       List<TreeNode> treeList = List2TreeList.buildTreeUseList(list, 0l);
       System.out.println(treeList);
       return treeList;
   }
}

TreeList结果格式

转换后的集合数据格式

[
   {
       "id": 370000,
       "label": "山东省",
       "pid": 0,
       "children": [
           {
               "id": 370100,
               "label": "济南市",
               "pid": 370000,
               "children": [
                   {
                       "id": 370102,
                       "label": "历下区",
                       "pid": 370100,
                       "children": []
                   },
                   {
                       "id": 370103,
                       "label": "市中区",
                       "pid": 370100,
                       "children": []
                   }
               ]
           },
           {
               "id": 370200,
               "label": "青岛市",
               "pid": 370000,
               "children": [
                   {
                       "id": 370202,
                       "label": "市南区",
                       "pid": 370200,
                       "children": []
                   }
               ]
           },
           {
               "id": 370300,
               "label": "淄博市",
               "pid": 370000,
               "children": []
           },
           {
               "id": 371300,
               "label": "临沂市",
               "pid": 370000,
               "children": []
           }
       ]
   }
]

来源:https://blog.csdn.net/m0_48420795/article/details/126391100

标签:Java,List,集合,树形,结构
0
投稿

猜你喜欢

  • c#开发cad预览图块步骤详解

    2022-12-30 10:49:07
  • MyBatis查询时属性名和字段名不一致问题的解决方法

    2023-10-23 16:56:36
  • 一文详解Java抽象类到底有多抽象

    2023-08-27 01:41:26
  • 深入分析JAVA流程控制语句

    2023-11-20 10:48:32
  • java 进制转换实例详解

    2023-07-05 11:53:45
  • Java 栈与队列超详细分析讲解

    2023-08-15 01:09:07
  • 解决javaWEB中前后台中文乱码问题的3种方法

    2023-03-22 22:39:26
  • C#实现Base64处理的加密解密,编码解码示例

    2023-07-15 12:11:31
  • 解决Springboot get请求是参数过长的情况

    2023-11-27 16:45:37
  • Java读取Properties文件的七种方法的总结

    2022-08-12 13:27:18
  • SpringMVC 参数绑定相关知识总结

    2022-06-05 12:50:54
  • Android ContentProvider实现获取手机联系人功能

    2023-08-07 00:52:27
  • 浅谈Java线程Thread之interrupt中断解析

    2023-07-19 09:25:11
  • 深入解析java中的locale

    2023-11-09 18:14:20
  • 通过java.util.TreeMap源码加强红黑树的理解

    2021-07-27 08:45:59
  • 聊聊Java的switch为什么不支持long

    2023-08-24 17:35:14
  • 一看就懂的Android APP开发入门教程

    2023-07-18 04:10:41
  • Spring Boot整合流控组件Sentinel的场景分析

    2023-06-22 19:27:53
  • SpringMVC+MyBatis 事务管理(实例)

    2022-07-30 11:04:40
  • 基于jni调用时,jvm报错问题的深入分析

    2022-08-08 17:21:55
  • asp之家 软件编程 m.aspxhome.com