java8 树类型转换

复制代码
/**
 * 创建树
 *
 * @param list       列表
 * @param parentRoot 父根
 * @return {@link List}<{@link TreeLabelDto}>
 */
public static List<TreeLabelDto> createTree(List<TreeLabelDto> list, String parentRoot) {
    Map<String, List<TreeLabelDto>> rootList = list.stream().filter(treeDto -> !treeDto.getParentId().equals(parentRoot)).collect(Collectors.groupingBy(treeDto -> treeDto.getParentId()));
    list.forEach(treeDto -> treeDto.setChildren(rootList.get(treeDto.getId())));
    return list.stream().filter(treeDto -> treeDto.getParentId().equals(parentRoot)).collect(Collectors.toList());
}

package com.stock.mr.enterprise.common.dto;

import net.logstash.logback.encoder.org.apache.commons.lang3.builder.EqualsBuilder;

import net.logstash.logback.encoder.org.apache.commons.lang3.builder.HashCodeBuilder;

import org.apache.pulsar.shade.com.fasterxml.jackson.annotation.JsonBackReference;

import org.apache.pulsar.shade.com.fasterxml.jackson.annotation.JsonManagedReference;

import java.io.Serializable;

import java.util.List;

public class TreeLabelDto implements Serializable {

private static final long serialVersionUID = 2434282106176669573L;

private String id;

private String parentId;

@JsonBackReference

private TreeLabelDto parent;

@JsonManagedReference

private List children;

private String name;

private String label;

private int level;

private int sort;

private String rootId;

private String type;

private boolean isLeaf;

private String description;

private String flag;

private String value;

复制代码
public TreeLabelDto() {
}

public TreeLabelDto(String id, String parentId, String name) {
    this.id = id;
    this.parentId = parentId;
    this.name = name;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getId() {
    return this.id;
}

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

public String getParentId() {
    return this.parentId;
}

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

public void setRootId(String rootId) {
    this.rootId = rootId;
}

public String getRootId() {
    return this.rootId;
}

public TreeLabelDto getParent() {
    return this.parent;
}

public void setParent(TreeLabelDto parent) {
    this.parent = parent;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getLabel() {
    return label;
}

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

public int getLevel() {
    return this.level;
}

public void setLevel(int level) {
    this.level = level;
}

public String getType() {
    return this.type;
}

public List<TreeLabelDto> getChildren() {
    return this.children;
}

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

public void setType(String type) {
    this.type = type;
}

public boolean isLeaf() {
    return this.isLeaf;
}

public void setLeaf(boolean isLeaf) {
    this.isLeaf = isLeaf;
}

public int getSort() {
    return this.sort;
}

public void setSort(int sort) {
    this.sort = sort;
}

public String getFlag() {
    return this.flag;
}

public void setFlag(String flag) {
    this.flag = flag;
}

public int hashCode() {
    return (new HashCodeBuilder(17, 37)).append(this.id).append(this.parentId).toHashCode();
}

public boolean equals(Object obj) {
    boolean isEqual = false;
    if (obj != null && TreeLabelDto.class.isAssignableFrom(obj.getClass())) {
        TreeLabelDto TreeLabelDto = (TreeLabelDto)obj;
        isEqual = (new EqualsBuilder()).append(this.getId(), TreeLabelDto.getId()).append(this.getParentId(), TreeLabelDto.getParentId()).isEquals();
    }

    return isEqual;
}

public String toString() {
    return "Node {id=" + this.id + ", parentId=" + this.parentId + ", children=" + this.children + ", name=" + this.name + ", level =" + this.level + "}";
}

public String getValue() {
    return this.value;
}

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

}

相关推荐
考虑考虑11 分钟前
Springboot捕获feign抛出的异常
spring boot·后端·spring
百锦再18 分钟前
Android Studio 实现自定义全局悬浮按钮
android·java·ide·app·android studio·安卓
百锦再20 分钟前
Android Studio 项目文件夹结构详解
android·java·ide·ios·app·android studio·idea
阿达King哥36 分钟前
Java虚拟机(JVM)平台无关?相关?
java·jvm
不是AI42 分钟前
【Java编程】【计算机视觉】一种简单的图片加/解密算法
java·算法·计算机视觉
森叶1 小时前
Java NIO & Java 虚拟线程(微线程)与 Go 协程的运行原理不同 为何Go 能在低配机器上承接10万 Websocket 协议连接
java·websocket·nio
程序员小蘇1 小时前
一天一个java知识点----Tomcat与Servlet
java·servlet·tomcat
Moso_Rx1 小时前
JavaEE——线程安全
java·安全·java-ee
〆、风神1 小时前
Spring Boot实战:基于策略模式+代理模式手写幂等性注解组件
spring boot·代理模式·策略模式
岁岁岁平安2 小时前
SpringMVC入门学习总结(2025.04.16)
java·spring·java-ee·mvc·springmvc