前言
很久不写业务接口, 最近刚好遇到需要实现树结构, 做下简单总结
逻辑实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package com.easyliao.demo;
import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data;
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors;
public class TreeTest {
@Data public static class Node { private Integer id; private String name; private Integer pid; private List<Node> children;
public Node(Integer id, String name, Integer pid) { this.id = id; this.name = name; this.pid = pid; } }
public static void main(String[] args) throws Exception{ Node p0 = new Node(1, "p00", 0); Node p1 = new Node(2, "p01", 0); Node p2 = new Node(3, "p10", 1); Node p3 = new Node(4, "p11", 1); Node p4 = new Node(5, "p20", 3); List<Node> nodes = Arrays.asList(p0, p1, p2, p3, p4); List<Node> tree = buildTree(nodes);
ObjectMapper objectMapper = new ObjectMapper(); System.out.println(objectMapper.writeValueAsString(tree));
}
public Node buildOneNodeTree(List<Node> pidList){ Map<Integer,List<Node>> pidListMap = pidList.stream().collect(Collectors.groupingBy(Node::getPid)); pidList.forEach(item->item.setChildren(pidListMap.get(item.getId()))); return pidListMap.get(0).get(0); }
public static List<Node> buildTree(List<Node> pidList){ Map<Integer,List<Node>> pidListMap = pidList.stream().collect(Collectors.groupingBy(Node::getPid)); pidList.forEach(item->item.setChildren(pidListMap.get(item.getId()))); return pidListMap.get(0); }
}
|
简单抽象
我们这里做一个简单工具类
- 首先我们抽象一个接口, 所有实现Tree结构的对象需要实现该接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
public interface TreeNode<T> {
T getId();
T getPid();
void setChildren(List<? extends TreeNode<T>> children);
List<? extends TreeNode<T>> getChildren(); }
|
- 定义基本实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
@Data public class Node implements TreeNode{
private Integer id; private String name; private Integer pid; private List<Node> children;
public Node(Integer id, String name, Integer pid) { this.id = id; this.name = name; this.pid = pid; }
@Override public Object getId() { return id; }
@Override public Object getPid() { return pid; }
@Override public List<? extends TreeNode> getChildren() { return children; }
@Override public void setChildren(List children) { this.children = children; } }
|
- 工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| * @author cuishiying * @date 2021-01-22 */ public class TreeBuilder {
public static List<? extends TreeNode> build(List<? extends TreeNode> nodes) { Map<Object, ? extends List<? extends TreeNode>> pidListMap = nodes.stream().collect(Collectors.groupingBy(TreeNode::getPid)); nodes.forEach(item->item.setChildren(pidListMap.get(item.getId()))); return pidListMap.get(0); } }
|
- 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public class TreeTest {
public static void main(String[] args) throws Exception { Node p0 = new Node(1, "p00", 0); Node p1 = new Node(2, "p01", 0); Node p2 = new Node(3, "p10", 1); Node p3 = new Node(4, "p11", 1); Node p4 = new Node(5, "p20", 3); List<Node> nodes = Arrays.asList(p0, p1, p2, p3, p4); List<Node> tree = (List<Node>) TreeBuilder.build(nodes);
ObjectMapper objectMapper = new ObjectMapper(); System.out.println(objectMapper.writeValueAsString(tree)); } }
|
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| [{ "id": 1, "name": "p00", "pid": 0, "children": [{ "id": 3, "name": "p10", "pid": 1, "children": [{ "id": 5, "name": "p20", "pid": 3, "children": null }] }, { "id": 4, "name": "p11", "pid": 1, "children": null }] }, { "id": 2, "name": "p01", "pid": 0, "children": null }]
|
最后
欢迎大家关注公众号【当我遇上你】支持我。