package com.ropz.demo.utils;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ropz.demo.entity.Node;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TreeUtils {
/**
* @Description: 递归list转tree
* @Param: list
* @Param: id 主键id
* @Param: pid 父级id
* @Param: childName 子节点名称
* @return: Tree
* @Author: lixy
* @Date: 2023/8/24 0024 16:51
*/
public static JSONArray listToTree(List<Map<String,Object>> list, String id, String pid, String childName){
JSONArray arr = JSONArray.parseArray(JSON.toJSONString(list));
JSONArray r = new JSONArray();
JSONObject hash = new JSONObject();
//将数组转为Object的形式,key为数组中的id
for (Object o : arr) {
JSONObject json = (JSONObject) o;
//将id作为key存入JSON Object
hash.put(json.getString(id), json);
}
//遍历结果集
for (Object o : arr) {
//单条记录
JSONObject aVal = (JSONObject) o;
//在hash中取出key为单条记录中pid的值
JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
//如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
if (hashVP != null) {
//检查是否有child属性
if (hashVP.get(childName) != null) {
JSONArray ch = (JSONArray) hashVP.get(childName);
ch.add(aVal);
hashVP.put(childName, ch);
} else {
JSONArray ch = new JSONArray();
ch.add(aVal);
hashVP.put(childName, ch);
}
} else {
r.add(aVal);
}
}
return r;
}
/**
* @Description: List<Map<String, Object>>类型 通过Stream流list转tree
* @Param: list
* @Param: childName 子节点名称
* @return: Tree
* @Author: lixy
* @Date: 2023/8/24 0024 16:51
*/
public static List<Map<String, Object>> listToTreeByStream(List<Map<String,Object>> list, String childName){
//第一步过滤pid=0的节点 第二步进行分组 根pid为key
Map<Integer,List<Map<String, Object>>> map = list.stream().filter(item -> Convert.toInt(item.get("pid")) != 0).
collect(Collectors.groupingBy(item -> Convert.toInt(item.get("pid"))));
//循环设置对应的子节点(根id = pid) 上一步以pid为Key 所以就直接循环获取
list.forEach(item -> item.put(childName,map.get(Convert.toInt(item.get("id")))));
//过滤第一层不是Pid为零的数 也就是没有根节点的数据
return list.stream().filter(item -> Convert.toInt(item.get("pid")) == 0).collect(Collectors.toList());
}
/**
* @Description: List<Java对象>类型 通过Stream流list转tree
* @Param: list
* @return: Tree
* @Author: lixy
* @Date: 2023/8/24 0024 16:51
*/
public static String buildTree(List<Node> nodes){
Map<Integer, List<Node>> nodeMap = nodes.stream().filter(node -> node.getPid() != 0).
collect(Collectors.groupingBy(Node::getPid));
nodes.forEach(node -> node.setTreeNode(nodeMap.get(node.getId())));
List<Node> collect = nodes.stream().filter(node -> node.getPid() == 0).collect(Collectors.toList());
return JSON.toJSONString(collect);
}
//模拟数据
public static String handleTreeVo(){
Node one = new Node(1,"one",0);
Node two = new Node(2,"two",1);
Node three = new Node(3,"three",2);
Node four = new Node(4,"four",1);
Node five = new Node(5,"five",4);
Node six = new Node(6,"six",0);
List<Node> nodes = Arrays.asList(one,two, three,four,five,six);
return buildTree(nodes);
}
//测试
public static void main(String[] args) {
System.out.println(handleTreeVo());
}
@Data
class Node {
private Integer id;
private String name;
private Integer pid;
private List<Node> treeNode = new ArrayList<>();
public Node(Integer id, String name, Integer pid) {
this.id = id;
this.name = name;
this.pid = pid;
}
}
}
Stream流和递归两个方式向前端返回树结构
于 2023-08-24 17:32:10 首次发布