在日常的开发工作中,时常会遇到树形结构的封装,比如:树形结构的菜单数据、部门数据等等。最近工作中,指标的树形结构封装场景频繁,比如:校验每个层级的指标权重之和要等于100,指标的满树校验等,接下来我们就来看一下我的思路。
一、准备数据
(1)准备一个指标实体类
@Data public class Indicator { private String code; private String parentCode; private String name; private Integer weight; private List<Indicator> children = Lists.newArrayList(); public Indicator() { } public Indicator(String code, String parentCode, String name, Integer weight) { this.code = code; this.parentCode = parentCode; this.name = name; this.weight = weight; } }