基于List封装递归树及查找当前节点下所有子节点


前言

在Java日常开发中,经常遇到需要组装数据格式为多层级递归树的形式给前端使用,根据具体的业务场景和数据结构封装树的工具类也是各种各样,那么有没有一种通用、简洁、容易理解且高效的方式实现这样的业务场景呢?


一、List常用业务场景的两种数据类型?

一般根据业务场景常用的数据结构类型有List<Entity>和List<Map>这两种数据类型,那么接下来针对这两种类型分别封装树结构

二、封装Tree

1.针对List类型为实体类封装树

实体类代码如下(示例):

import lombok.Data;
import java.util.List;

@Data
public class Entity {
	private String gid; -- 主键
	private String name; -- 名称
	private String parentId; -- 所属父级
	private List<Entity> children; -- 子集合
}

封装Tree代码如下(示例):

import java.util.*;
import org.springframework.util.CollectionUtils;

public class TreeUtil {

	/**
	* 构建树
	* @param parent: 父级目录 入口初始化传 null
	* @param all : 要封装的所有数据集合,包含上下级关系
	*/
	public static List<Entity> buildTree(Entity parent, List<Entity> all) {
		
		if(CollectionUtils.isEmpty(all)) {
			return Collections.emptyList();
		}
		
		if(Objects.isNull(parent)) {
			return all.stream().filter(e -> "0".equals(e.getParentId())).filter(a -> {
				a.setChildren(buildTree(a,all));
				return true;
			}).collect(Collectors.toList());
		}else {
			return all.stream().filter(e -> parent.getGid().equals(e.getParentId())).filter(a -> {
				a.setChildren(buildTree(a,all));
				return true;
			}).collect(Collectors.toList());
		}
	}
}

2.针对List类型为Map封装树

封装Tree代码如下(示例):

import java.util.*;
import org.springframework.util.CollectionUtils;

public class TreeUtil {
	
	/**
	* 构建树
	* @param root : 父级目录 入口初始化传 null
	* @param all : 要封装的所有数据集合,包含上下级关系
	*/
	public static List<Map> buildTree(Map root, List<Map> all) {
		
		if(CollectionUtils.isEmpty(all)) {
			return Collections.emptyList();
		}
		
		if(Objects.isNull(root)) {
			return all.stream().filter(e -> "0".equals(e.get("parent_id")))
					.map(a -> {
						a.put("children", buildTree(a,all));
						return a;
					}
			).collect(Collectors.toList());
		}else {
			return all.stream().filter(e -> root.get("gid").equals(e.get("parent_id")))
					.map(a -> {
						a.put("children", buildTree(a,all));
						return a;
					}
			).collect(Collectors.toList());
		}
	}
}

3.查找当前节点下所有子节点

import java.util.*;
import org.springframework.util.CollectionUtils;

public class TreeUtil {

	/**
	* 查找当前节点下所有子节点
	* @param nodeId : 当前节点
	* @param all : 所有节点数据查找目标集合
	* @param result : 查找的当前节点及节点所有子节点集合
	*/
	public static void findNodeChild(String nodeId, List<Map> all, List<String> result) {
		// 1、查找当前节点下的所有子节点
		List<Map> child = all.stream().filter(a -> nodeId.equals(a.get("parent_id"))).collect(Collectors.toList());
		// 2、添加当前节点
		result.add(nodeId);
		// 3、递归查找
		if(!CollectionUtils.isEmpty(child)) {
			child.stream().forEach(g -> {
				findNodeChild(g.get("gid").toString(),all,result);
			});
		}
	}
}

总结

调用方式:以上封装默认顶级父节点的parent_id赋值为"0"

// 1、调用树
List<Entity> all = jdbc; // 从数据库获取的需要封装的业务数据包含上下级关系那种
List<Entity> result = TreeUtil.buildTree(null,all); // 返回前端的树结构数据
// 2、查找当前节点及其下所有子节点
List<String> result = Lists.newArrayList(); // 当前节点及其下所有子节点gid集合
String nodeId = gid; // 前端传参当前节点gid
TreeUtil.findNodeChild(nodeId,all,result);

记录到此结束,有问题欢迎评论留言讨论,学习路上有你有我~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值