递归实现获取前端tree json格式数据

黎震博
2023-12-01

问题描述

为了表示各个业务实体的层级关系,后台系统中很多表结构设计的时候遵循类似下面这种结构。

idpidname

然而前端需要的数据结构为Tree JSON结构如下:

[{
	"id": 1,
	"name": "女装",
	"children": [{
		"id": 3,
		"name": "女士裙装",
		"children": [{
			"id": 5,
			"name": "背带裙",
			"children": []
		}]
	}, {
		"id": 4,
		"name": "女士上装",
		"children": [{
			"id": 6,
			"name": "羊毛衫",
			"children": []
		}]
	}]
}, {
	"id": 2,
	"name": "男装",
	"children": []
}]

思路

递归思路:

  • 明确递归终止条件(list中筛选出pid的结果list为空)
  • 给出递归终止时的处理办法(不处理)
  • 提取重复的逻辑(拼装tree节点,添加到tree节点List中)

首先确认递归函数的入参:

  • 需要查询出来的扁平的业务实体List
  • 需要List结果实体集合,实体里面三个字段(可自定义字段名)id, name, children。这里为了简便用了Map
  • 查询当前层级的pid

代码实现

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

public class ListToTreeTest {

    public static void main(String[] args) throws Exception {
        List<Item> list = getList();
        ArrayList<LinkedHashMap<String, Object>> tree = new ArrayList<>();
        loadTree(list, tree, 0l);
        String s = new ObjectMapper().writeValueAsString(tree);
        System.out.println(s);
    }

    public static void loadTree(List<Item> list, List<LinkedHashMap<String, Object>> listTreeNode, Long pid) {
        LinkedHashMap<String, Object> tmpMap = null;
        //从list中筛选出特定pid的Item集合
        List<Item> tmpList = list.stream().filter(x -> x.getPid() == pid).collect(Collectors.toList());
        for(Item item : tmpList) {
            tmpMap = new LinkedHashMap<>();
            tmpMap.put("id", item.getId());
            tmpMap.put("name", item.getName());
            tmpMap.put("children", new ArrayList<LinkedHashMap<String,Object>>());
            listTreeNode.add(tmpMap);
            //children中的每一项递归查询,此时children中的每一项的pid为上面的id
            loadTree(list, (List<LinkedHashMap<String, Object>>) tmpMap.get("children"), (Long) tmpMap.get("id"));
        }
    }
	//获取测试数据
    public static List<Item> getList() {
        Item parent1 = new Item();
        parent1.setId(1l);
        parent1.setName("女装");
        parent1.setPid(0l);

        Item parent2 = new Item();
        parent2.setId(2l);
        parent2.setName("男装");
        parent2.setPid(0l);

        Item parent3 = new Item();
        parent3.setId(3l);
        parent3.setName("女士裙装");
        parent3.setPid(1l);

        Item parent4 = new Item();
        parent4.setId(4l);
        parent4.setName("女士上装");
        parent4.setPid(1l);

        Item parent5 = new Item();
        parent5.setId(5l);
        parent5.setName("背带裙");
        parent5.setPid(3l);

        Item parent6 = new Item();
        parent6.setId(6l);
        parent6.setName("羊毛衫");
        parent6.setPid(4l);

        ArrayList<Item> items = new ArrayList<>();
        items.add(parent1);
        items.add(parent2);
        items.add(parent3);
        items.add(parent4);
        items.add(parent5);
        items.add(parent6);
        return items;
    }
}
//表结构实体
class Item {
    private Long id;

    private String name;

    private Long pid;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getPid() {
        return pid;
    }
    public void setPid(Long pid) {
        this.pid = pid;
    }
}


 类似资料: