首页 > 编程语言 >java 树结构构建

java 树结构构建

时间:2022-08-17 11:37:09浏览次数:50  
标签:java String 树结构 void Long 构建 private return public

import io.swagger.annotations.ApiModel;

import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

/**
* 组织树
*/
@ApiModel(value = "组织树对象")
public class UnitTreeDTO {
/**
* 组织ID
*/
@ApiModelProperty("组织Id")
private String unitId;

/**
* 组织名称
*/
@ApiModelProperty("组织树名称")
private String unitName;

/**
* 组织全名称下划线拼接
*/
private String unitFullName;

/**
* 组织全ID下划线拼接
*/
private String unitFullId;

/**
* 父组织ID
*/
private String parentId;

/**
* 组织层次
*/
@ApiModelProperty("组织树层级")
private Long orgTier;

/**
* 组织类型 1:区域,2:项目
*/
private Long unitType;

/**
* 原组织代码
*/
@ApiModelProperty("区域编码/项目编码")
private String unitCode;

/**
* 租户Id
*/
private Long corpId;

/**
* 作废标记(1删除0非删除)
*/
private Long isDelete;

/**
* 0为停用 1为启用
*/
private Long isEnable;

public List<UnitTreeDTO> childNode;

public void addChildNode(UnitTreeDTO dto) {
if (this.childNode == null) {
this.childNode = new ArrayList<>();
}
this.childNode.add(dto);
}

public String getUnitId() {
return unitId;
}

public void setUnitId(String unitId) {
this.unitId = unitId;
}

public String getUnitName() {
return unitName;
}

public void setUnitName(String unitName) {
this.unitName = unitName;
}

public String getUnitFullName() {
return unitFullName;
}

public void setUnitFullName(String unitFullName) {
this.unitFullName = unitFullName;
}

public String getUnitFullId() {
return unitFullId;
}

public void setUnitFullId(String unitFullId) {
this.unitFullId = unitFullId;
}

public String getParentId() {
return parentId;
}

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

public Long getOrgTier() {
return orgTier;
}

public void setOrgTier(Long orgTier) {
this.orgTier = orgTier;
}

public Long getUnitType() {
return unitType;
}

public void setUnitType(Long unitType) {
this.unitType = unitType;
}

public String getUnitCode() {
return unitCode;
}

public void setUnitCode(String unitCode) {
this.unitCode = unitCode;
}

public Long getCorpId() {
return corpId;
}

public void setCorpId(Long corpId) {
this.corpId = corpId;
}

public Long getIsDelete() {
return isDelete;
}

public void setIsDelete(Long isDelete) {
this.isDelete = isDelete;
}

public Long getIsEnable() {
return isEnable;
}

public void setIsEnable(Long isEnable) {
this.isEnable = isEnable;
}

public List<UnitTreeDTO> getChildNode() {
return childNode;
}

public void setChildNode(List<UnitTreeDTO> childNode) {
this.childNode = childNode;
}
}

 

============================结构构建==========================

//转换为树状类型
// 存放最终结果
List<UnitTreeDTO> result = new ArrayList<>();

Map<String, UnitTreeDTO> orderOfAmount = allUnitList.stream().collect(Collectors.toMap(UnitTreeDTO::getUnitId, a -> a, (k1, k2) -> k1));

for (UnitTreeDTO productType : allUnitList) {
// 子级通过父id获取到父级的类型
UnitTreeDTO parent = orderOfAmount.get(productType.getParentId());
if (parent == null) {
//如果没有找到父级,放入根目录
result.add(productType);
} else {
// 父级获得子级,再将子级放到对应的父级中
parent.addChildNode(productType);
}
}

 

标签:java,String,树结构,void,Long,构建,private,return,public
From: https://www.cnblogs.com/huangwentian/p/16594475.html

相关文章