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;
}

}

相关推荐
2202_7544215411 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
蓝染-惣右介13 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
小林想被监督学习14 分钟前
idea怎么打开两个窗口,运行两个项目
java·ide·intellij-idea
HoneyMoose16 分钟前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热18 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
是老余19 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar
crazy_wsp19 分钟前
IDEA怎么定位java类所用maven依赖版本及引用位置
java·maven·intellij-idea
.Ayang22 分钟前
tomcat 后台部署 war 包 getshell
java·计算机网络·安全·web安全·网络安全·tomcat·网络攻击模型
bjzhang7527 分钟前
SpringBoot开发——Maven多模块工程最佳实践及详细示例
spring boot·maven·maven多模块工程
一直学习永不止步27 分钟前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表