java实现以树状结构展示数据(递归思想)

引言:有时在实际项目中,可能会需要以树状结构来展示数据,并返回给前端,比如像部门的展示就有
多层级,以树状展示更清晰明了,这里以递归的方式来处理数据。
1.定义一个列表转树状工具类。

/**
 * 树状工具类
 */
public class TreeUtils {	
	static String root = "0";
	/**
	 * Primary key id
	 */
	static String id = "id";
	/**
	 * Parent node id
	 */
	static String pid = "pid";
	/**
	 * Child node collection
	 */
	static String children = "children";
	
	/**
	 * 列表转树状结构
	 * @param trees 
	 * @param Map
	 * @return result
	 * @throws IllegalAccessException result
	 */
	public  static List<Map<String,Object>>  ListToTreeMap(List<Map<String,Object>> trees) throws IllegalAccessException {
		//Root node collection
		List<Map<String,Object>> rootList = new ArrayList<Map<String,Object>>();
		HashMap<Object, List<Map<String,Object>>> pidAndTrees= new HashMap<>();
		for (Map<String,Object> t : trees) {
			t.put(children, new ArrayList<Map<String, Object>>());
            Object value=t.get(pid); 
			if(value!=null){
				if(pidAndTrees.get(value)!=null) {
					pidAndTrees.get(value).add(t);
				}else{
					pidAndTrees.put(value,new ArrayList<Map<String,Object>>(){{this.add(t);}});
				}
				if(Objects.equals(root, String.valueOf(value))) {
					rootList.add(t);
				}
			}
		}
		buildChilTreeMap(rootList,pidAndTrees);
		return rootList;
	}
	/**
	 * @param currentTrees 
	 * @param trees
	 * @throws IllegalAccessException
	 */
	private static  void  buildChilTreeMap(List<Map<String,Object>> currentTrees, HashMap<Object, List<Map<String,Object>>>  trees) throws IllegalAccessException {
		for (Map<String,Object> t : currentTrees) {
			Object currentId = t.get(id);
			if(trees.get(currentId)!=null){
				t.put(children, trees.get(currentId));
				buildChilTreeMap(trees.get(currentId),trees);
			}
		}
	}
}

2.定义一个测试Demo。

public class TreeDemo {
	public static void main(String[] args) throws IllegalAccessException {
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		list.add(new HashMap<String, Object>(){{put("id",1);put("pid",0);}});
		list.add(new HashMap<String, Object>(){{put("id",33);put("pid",34);}});
		list.add(new HashMap<String, Object>(){{put("id",2);put("pid",1);}});
		list.add(new HashMap<String, Object>(){{put("id",17);put("pid",1);}});
		list.add(new HashMap<String, Object>(){{put("id",19);put("pid",17);}});
		list.add(new HashMap<String, Object>(){{put("id",15);put("pid",2);}});
		list.add(new HashMap<String, Object>(){{put("id",5);put("pid",15);}});
		list.add(new HashMap<String, Object>(){{put("id",6);put("pid",15);}});
		list.add(new HashMap<String, Object>(){{put("id",7);put("pid",15);}});
		list.add(new HashMap<String, Object>(){{put("id",21);put("pid",15);}});
		list.add(new HashMap<String, Object>(){{put("id",8);put("pid",5);}});
		list.add(new HashMap<String, Object>(){{put("id",11);put("pid",5);}});
		list=TreeUtils.ListToTreeMap(list);
		System.err.println(list); 	
	}
}

3.返回数据:

[{
	children = [{
		children = [{
			children = [{
				children = [{
					children = [],
					pid = 5,
					id = 8
				}, {
					children = [],
					pid = 5,
					id = 11
				}],
				pid = 15,
				id = 5
			}, {
				children = [],
				pid = 15,
				id = 6
			}, {
				children = [],
				pid = 15,
				id = 7
			}, {
				children = [],
				pid = 15,
				id = 21
			}],
			pid = 2,
			id = 15
		}],
		pid = 1,
		id = 2
	}, {
		children = [{
			children = [],
			pid = 17,
			id = 19
		}],
		pid = 1,
		id = 17
	}],
	pid = 0,
	id = 1
}]

大家有兴趣可以试一试。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空下的星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值