tree树形实体类
@Data
public class TblLabelTree {
/**
* 主键
*/
private Long id;
/**
* 父级id
*/
private Long parentId;
/**
* 级别
*/
private Integer level;
/**
* 标签名字
*/
private String labelName;
/**
* 完整标签id
*/
private String labelParentId;
/**
* 完整标签名
*/
private String labelParentName;
/**
* 子集
*/
private List children = new ArrayList<>();
}
实现代码一 利用双map实现
/**
* 查询标签树
* @param type
* @return
*/
@Override
public List listTree(Integer type) {
TblLabelDTO tblLabel=new TblLabelDTO ();
tblLabel.setType(type);
// 获取所有数据
List tblLabels = selectTblLabel(tblLabel);
List labelTree = CollUtil.newArrayList();
for (TblLabel label : tblLabels) {
TblLabelTree labelVo = new TblLabelTree();
BeanUtil.copyProperties(label,labelVo);
labelTree.add(labelVo);
}
List tblLabelTree = treeIfy(labelTree);
return tblLabelTree;
}
/**
* 处理成树形结构
* @param labelList
* @return
*/
public List treeIfy( List labelList) {
Map originMap = new HashMap<>();
Map destinationMap = new HashMap<>();
for (TblLabelTree label : labelList) {
originMap.put(label.getId(), label);
destinationMap.put(label.getId(), label);
}
for (TblLabelTree label : labelList) {
TblLabelTree group = originMap.get(label.getParentId());
if (group != null) {
// map的put的值,其实就是将value值的引用指向放入value的对象,属于对象的引用,指针的变化,所以当这个对象在后期变化时,value值也会随之变化
originMap.get(label.getParentId()).getChildren().add(label);
// 只保留第一级数据
destinationMap.remove(label.getId());
}
}
// 处理成树形结构
List tree = new ArrayList<>();
Iterator iterator = destinationMap.values().iterator();
while (iterator.hasNext()) {
tree.add(iterator.next());
}
return tree;
}
实现代码二 JDK8stream流
/**
* 查询标签树
* @param type
* @return
*/
@Override
public List listTree(Integer type) {
TblLabelDTO tblLabel=new TblLabelDTO ();
tblLabel.setType(type);
// 获取所有数据
List tblLabels = selectTblLabel(tblLabel);
List labelTree = CollUtil.newArrayList();
for (TblLabel label : tblLabels) {
TblLabelTree labelVo = new TblLabelTree();
BeanUtil.copyProperties(label,labelVo);
labelTree.add(labelVo);
}
// 排除一级 根据父节点id 分组 获取所有的子节点元素
Map> collect = labelTree.stream()
.filter(Objects::nonNull)
.filter(o -> o.getParentId()!=0)
.collect(Collectors.groupingBy(TblLabelTree::getParentId));
// 添加子节点元素
for (TblLabelTree data : labelTree) {
Long code = data.getId();
List list = collect.get(code);
data.setChildren(list);
}
// 过滤 获取一级的所有元素
labelTree = labelTree.stream()
.filter(e -> e.getParentId()==0)
.collect(Collectors.toList());
// List tblLabelTree = treeIfy(labelTree);
return labelTree;
}
结果显示
{
"msg": "操作成功",
"code": 200,
"data": [
{
"id": 1,
"parentId": 0,
"level": 1,
"labelName": "这是第一级别标签",
"labelParentId": "1",
"labelParentName": "这是第一级别标签",
"children": [
{
"id": 2,
"parentId": 1,
"level": 2,
"labelName": "三级标签6",
"labelParentId": "1,2",
"labelParentName": "这是第一级别标签,三级标签6",
"children": []
},
{
"id": 3,
"parentId": 1,
"level": 2,
"labelName": "三级标签8",
"labelParentId": "1,3",
"labelParentName": "这是第一级别标签,三级标签8",
"children": []
},
{
"id": 4,
"parentId": 1,
"level": 2,
"labelName": "二级标签添加",
"labelParentId": "4",
"labelParentName": "这是第一级别标签",
"children": [
{
"id": 5,
"parentId": 4,
"level": 3,
"labelName": "二级标签的子标签1",
"labelParentId": "4,5",
"labelParentName": "这是第一级别标签,二级标签的子标签1",
"children": []
},
{
"id": 6,
"parentId": 4,
"level": 3,
"labelName": "二级标签的子标签2",
"labelParentId": "4,6",
"labelParentName": "这是第一级别标签,二级标签的子标签2",
"children": []
}
]
},
{
"id": 10,
"parentId": 1,
"level": 2,
"labelName": "二级标签添加2",
"labelParentId": "1,10",
"labelParentName": "这是第一级别标签,二级标签添加2",
"children": [
{
"id": 11,
"parentId": 10,
"level": 3,
"labelName": "二级标签的子标签12",
"labelParentId": "1,10,11",
"labelParentName": "这是第一级别标签,二级标签添加2,二级标签的子标签12",
"children": []
}
]
}
]
}
]
}