尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

树形结构转换工具类

树形结构转换工具类
📅 发布时间:2026/6/19 14:49:21

项目中使用了很多树状结构,为了方便使用开发一个通用的工具类。

使用工具类的时候写一个类基础BaseNode,如果有个性化字段添加到类里面,然后就可以套用工具类。

工具类会将id和pid做关联返回一个树状结构的集合。使用了hutool的工具包判空。

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.25</version>
</dependency>

TreeUtil.java

import cn.hutool.core.util.ObjUtil;
import com.iccb.BaseNode;import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;public class TreeUtil {public static <T extends BaseNode<T>> List<T> toTree(List<T> treeNodeList) {//数据按照pid分组Map<Long, List<T>> map = treeNodeList.stream().collect(Collectors.groupingBy(T :: getPid,LinkedHashMap::new,Collectors.toList()));//找出所有的根节点,pid=-1的为根节点List<T> areaList = map.get(-1L);if(ObjUtil.isEmpty(areaList)){return new ArrayList<>();}for (T areaVO : areaList) {forEach(map, areaVO);}return areaList;}private static  <T extends BaseNode<T>> void forEach(Map<Long, List<T>> collect, T areaVO) {List<T> nodeList = collect.get(areaVO.getId());if (collect.get(areaVO.getId()) == null) {return;}areaVO.setChildren(nodeList);for (T node : nodeList) {forEach(collect, node);}}}

BaseNode.java

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.util.List;/*** @author admin*/
@Data
public class BaseNode<T> implements Serializable {private static final long serialVersionUID = 1L;@JsonFormat(shape = JsonFormat.Shape.STRING)private Long id;@JsonFormat(shape = JsonFormat.Shape.STRING)private Long pid;private String value;private List<T> children;
}

 使用范例

返回树状结构的地区数据

1)首先构建数据VO

@EqualsAndHashCode(callSuper = true)
@Data
public class AreaVO extends BaseNode<AreaVO> {private static final long serialVersionUID = 1L;private String areaCode;private String label;private String level;private String lnglat;}

2)查询地区数据

List<Area> list = areaService.list();

3) 构建数据VO 或者 直接使用mapper查询用VO封装

List<AreaVO> collect = list.stream().map(i -> {AreaVO areaVO = BeanUtil.copyProperties(i, AreaVO.class);areaVO.setLabel(i.getAreaName());areaVO.setValue(String.valueOf(i.getId()));areaVO.setLevel(i.getAreaLevel());areaVO.setPid(i.getParentId());return areaVO;
}).collect(Collectors.toList());

4) 使用工具类构造数据结构

List<AreaVO> areaTree = TreeUtil.toTree(collect);

返回数据示例

查看代码
 {"code": 200,"msg": null,"data": [{"id": "4058069977868140544","value": "4058069977868140544","pid": "4058069977868148888","children": [{"id": "4058069978245627904","value": "4058069978245627904","pid": "4058069977868140544","children": [{"id": "4058069978530840576","value": "4058069978530840576","pid": "4058069978245627904","children": null,"areaCode": "110101000000","label": "东城区","level": "3","lnglat": null},{"id": "4058069978979631104","value": "4058069978979631104","pid": "4058069978245627904","children": null,"areaCode": "110106000000","label": "丰台区","level": "3","lnglat": null},{"id": "4058069979046739968","value": "4058069979046739968","pid": "4058069978245627904","children": null,"areaCode": "110107000000","label": "石景山区","level": "3","lnglat": null},{"id": "4058069979118043136","value": "4058069979118043136","pid": "4058069978245627904","children": null,"areaCode": "110108000000","label": "海淀区","level": "3","lnglat": null},{"id": "4058069979650719744","value": "4058069979650719744","pid": "4058069978245627904","children": null,"areaCode": "110114000000","label": "昌平区","level": "3","lnglat": null},{"id": "4058069979747188736","value": "4058069979747188736","pid": "4058069978245627904","children": null,"areaCode": "110115000000","label": "大兴区","level": "3","lnglat": null},{"id": "4058069979852046336","value": "4058069979852046336","pid": "4058069978245627904","children": null,"areaCode": "110116000000","label": "怀柔区","level": "3","lnglat": null},{"id": "4058069980284059648","value": "4058069980284059648","pid": "4058069978245627904","children": null,"areaCode": "110229000000","label": "延庆区","level": "3","lnglat": null}],"areaCode": "110100000000","label": "北京市","level": "2","lnglat": null}],"areaCode": "110000000000","label": "北京市","level": "1","lnglat": null},{"id": "4058069980422471680","value": "4058069980422471680","pid": "4058069977868148888","children": [{"id": "4058069980527329280","value": "4058069980527329280","pid": "4058069980422471680","children": [{"id": "4058069980627992576","value": "4058069980627992576","pid": "4058069980527329280","children": null,"areaCode": "120101000000","label": "和平区","level": "3","lnglat": null},{"id": "4058069980732850176","value": "4058069980732850176","pid": "4058069980527329280","children": null,"areaCode": "120102000000","label": "河东区","level": "3","lnglat": null},{"id": "4058069980841902080","value": "4058069980841902080","pid": "4058069980527329280","children": null,"areaCode": "120103000000","label": "河西区","level": "3","lnglat": null},{"id": "4058069981148086272","value": "4058069981148086272","pid": "4058069980527329280","children": null,"areaCode": "120106000000","label": "红桥区","level": "3","lnglat": null},{"id": "4058069981252943872","value": "4058069981252943872","pid": "4058069980527329280","children": null,"areaCode": "120110000000","label": "东丽区","level": "3","lnglat": null},{"id": "4058069981357801472","value": "4058069981357801472","pid": "4058069980527329280","children": null,"areaCode": "120111000000","label": "西青区","level": "3","lnglat": null}],"areaCode": "120100000000","label": "天津市","level": "2","lnglat": null}],"areaCode": "120000000000","label": "天津市","level": "1","lnglat": null},{"id": "4461100398823866368","value": "4461100398823866368","pid": "4058069977868148888","children": [{"id": "4461100398974861312","value": "4461100398974861312","pid": "4461100398823866368","children": null,"areaCode": "820100000000","label": "澳门半岛","level": "2","lnglat": null},{"id": "4461100399113273344","value": "4461100399113273344","pid": "4461100398823866368","children": null,"areaCode": "820200000000","label": "离岛","level": "2","lnglat": null}],"areaCode": "820000000000","label": "澳门特别行政区","level": "1","lnglat": null}]
}

 

相关新闻

  • 2025年长沙心理咨询机构专家团队排名,在线/线上心理咨询公司排行
  • .net 行不行?在线客服系统成功支持客户双11大促,21客服在线,高峰超300会话并发
  • Cisco Secure Email and Web Manager Virtual 16.0.2 MD - 集中管理思科安全设备

最新新闻

  • SAP BOM查询实战:从正查到反查的完整指南
  • 【2026年6月】热水离心泵厂家推荐指南 - 多才菠萝
  • Python图片压缩方法全解:从入门到进阶
  • 【JAVA毕设源码分享】基于SpringBoot的中华传统文化网站(程序+文档+代码讲解+一条龙定制)
  • 全国学历提升继续教育学习体验实录
  • 验证码绕过实战:从Pikachu靶场剖析客户端与服务端漏洞原理

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号