Java:封装树结构

实体类

public class DictTreeselectVO {
    private String value;
    private String label;
    /**
     * 节点
     */
    private String parentId;
    private List<DictTreeselectVO> children = new ArrayList<DictTreeselectVO>();

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public List<DictTreeselectVO> getChildren() {
        return children;
    }

    public void setChildren(List<DictTreeselectVO> children) {
        this.children = children;
    }
}

返回结果

public class TreeSelectT implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 节点ID */
    private String id;

    /** 节点名称 */
    private String label;

    /** 子节点 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelectT> children;

    public TreeSelectT()
    {

    }

    public TreeSelectT(DictTreeselectVO dictTreeselectVO)
    {
        this.id = dictTreeselectVO.getValue();
        this.label = dictTreeselectVO.getLabel();
        this.children = dictTreeselectVO.getChildren().stream().map(TreeSelectT::new).collect(Collectors.toList());
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<TreeSelectT> getChildren() {
        return children;
    }

    public void setChildren(List<TreeSelectT> children) {
        this.children = children;
    }
}

工具类

/**
* 创建树结构
* @param treeselectList
* @return
*/
public static List<TreeSelectT> buildTreeSelect(List<DictTreeselectVO> treeselectList){
    List<DictTreeselectVO> treeselectVOList = buildDictTreeselect(treeselectList);
    return treeselectVOList.stream().map(TreeSelectT::new).collect(Collectors.toList());
}

/**
* 构建前端所需要树结构
* @param treeselectList
* @return
*/
private static List<DictTreeselectVO> buildDictTreeselect(List<DictTreeselectVO> treeselectList){
    List<DictTreeselectVO> returnList = new ArrayList<DictTreeselectVO>();
    List<String> tempList = treeselectList.stream().map(DictTreeselectVO::getValue).collect(Collectors.toList());
    for (DictTreeselectVO dictTreeselectVO : treeselectList) {
        // 如果是顶级节点, 遍历该父节点的所有子节点
        if (!tempList.contains(dictTreeselectVO.getParentId())) {
            recursionFn(treeselectList, dictTreeselectVO);
            returnList.add(dictTreeselectVO);
        }
    }
    if (returnList.isEmpty()){
        returnList = treeselectList;
    }
    return returnList;
}

/**
 * 递归列表
 * @param list
 * @param t
 */
private static void recursionFn (List<DictTreeselectVO> list, DictTreeselectVO t){
    // 得到子节点列表
    List<DictTreeselectVO> childList = getChildList(list, t);
    t.setChildren(childList);
    for (DictTreeselectVO tChild : childList) {
        if (hasChild(list, tChild)){
            recursionFn(list, tChild);
        }
    }
}

/**
 * 得到子节点列表
 * @param list
 * @param t
 * @return
 */
private static List<DictTreeselectVO> getChildList(List<DictTreeselectVO> list, DictTreeselectVO t)
{
    List<DictTreeselectVO> tlist = new ArrayList<DictTreeselectVO>();
    Iterator<DictTreeselectVO> it = list.iterator();
    while (it.hasNext())
    {
        DictTreeselectVO n = (DictTreeselectVO) it.next();
        if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getValue()))
        {
            tlist.add(n);
        }
    }
    return tlist;
}

/**
 * 判断是否有子节点
 */
private static boolean hasChild(List<DictTreeselectVO> list, DictTreeselectVO t){
    return getChildList(list, t).size() > 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Monly21

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

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

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

打赏作者

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

抵扣说明:

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

余额充值