如何将任意List转化成指定List树形结构
权限表PermissionDTO
/**
* <pre>
* 权限表 返回数据模型
* </pre>
*
* @author xx
* @date 2021-07-07
*/
@Data
@ApiModel(value = "PermissionDto", description = "权限表返回数据模型")
public class PermissionDto{
@ApiModelProperty(value = "主键")
@Excel(name="主键",cellType = Excel.ColumnType.STRING)
private Long id;
@ApiModelProperty(value = "描述")
@Excel(name="描述",cellType = Excel.ColumnType.STRING)
private String remark;
@ApiModelProperty(value = "父ID")
@Excel(name="父ID",cellType = Excel.ColumnType.STRING)
private Long parentId;
@ApiModelProperty(value = "下级权限")
private List<PermissionDto> children;
}
树TreeDTO
/**
* @author xx
*/
@ApiModel(value = "TreeDto", description = "树状数据返回数据模型")
public class TreeDto {
@ApiModelProperty(value = "数据标题")
private String title;
@ApiModelProperty(value = "数据key")
private String key;
@ApiModelProperty(value = "下级数据")
private List<TreeDto> children;
public TreeDto() { }
public TreeDto(PermissionDto permissionDto) {
this.key = permissionDto.getId().toString();
this.title = permissionDto.getName();
this.children = permissionDto.getChildren().stream().map(TreeDto::new).collect(Collectors.toList());
}
public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key = key;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public List<TreeDto> getChildren()
{
return children;
}
public void setChildren(List<TreeDto> children)
{
this.children = children;
}
}
转化代码
private List<TreeDto> getTreeDtos() {
/**
* service层生成的树形数据
*/
List<PermissionDto> permissionDtoList = permissionService.getTree(null);
return permissionDtoList.stream().map(TreeDto::new).collect(Collectors.toList());
}