package com.csw.shuanfa.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TreeBuilder {
/**
* parent Id 是空
*
* @param nodes
* @return
*/
public static List<NodeEntity> buildTree2(List<NodeEntity> nodes) {
Map<Integer, NodeEntity> nodeMap = new HashMap<>();
List<NodeEntity> roots = new ArrayList<>();
//1
firstChange(nodes);
//2
getNodeMap(nodes, nodeMap);
for (NodeEntity node : nodes) {
Integer parentId = node.getParentId();
NodeEntity parent = nodeMap.get(parentId);
if (parent != null) {
parent.getChildren().add(node);
} else {
roots.add(node);
}
}
return roots;
}
private static void firstChange(List<NodeEntity> nodes) {
for (NodeEntity node : nodes) {
//初始化子child集合
if (node.getChildren() == null) {
node.setChildren(new ArrayList<>());
}
if (node.getChildren().size() > 0) {
firstChange(node.getChildren());
}
}
}
private static void getNodeMap(List<NodeEntity> nodes, Map<Integer, NodeEntity> nodeMap) {
for (NodeEntity node : nodes) {
nodeMap.put(node.getId(), node);
if (node.getChildren().size() > 0) {
getNodeMap(node.getChildren(), nodeMap);
}
}
}
/**
* 按照每页的id,获取相应的树杈
*
* @param nodes
* @param list
* @param res
*/
public static void getNodeRes(List<NodeEntity> nodes, List<Integer> list, List<NodeEntity> res) {
for (NodeEntity node : nodes) {
if (list.contains(node.getId())) {
res.add(node);
}
if (node.getChildren().size() > 0) {
getNodeRes(node.getChildren(), list, res);
}
}
}
public static void main(String[] args) {
List<NodeEntity> nodes = new ArrayList<>();
nodes.add(new NodeEntity(111, null, null));
nodes.add(new NodeEntity(2, 111, null));
nodes.add(new NodeEntity(3, 111, null));
nodes.add(new NodeEntity(5, 2, null));
nodes.add(new NodeEntity(4, 2, null));
nodes.add(new NodeEntity(6, 3, null));
nodes.add(new NodeEntity(7, 222, null));
nodes.add(new NodeEntity(8, 222, null));
nodes.add(new NodeEntity(9, 7, null));
nodes.add(new NodeEntity(222, 0, null));
nodes.add(new NodeEntity(10, 7, null));
nodes.add(new NodeEntity(11, 8, null));
nodes = buildTree2(nodes);
nodes.add(new NodeEntity(12, 7, null));
nodes.add(new NodeEntity(13, 8, null));
nodes.add(new NodeEntity(14, 11, null));
nodes.add(new NodeEntity(15, 11, null));
nodes = buildTree2(nodes);
System.out.println(nodes);
//模拟获取当前页id集合
//List<Long> collect = pageResult.getItems().stream().map(NodeEntity::getId).collect(Collectors.toList());
List<Integer> col = new ArrayList<>();
col.add(111);
col.add(5);
col.add(6);
col.add(11);
//假设上面的树里面是根据上面的4条数据递归查询出来的子集合封装成的树。现在一页数据就是4条
List<NodeEntity> res = new ArrayList<>();
getNodeRes(nodes, col, res);
System.out.println(res);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
class NodeEntity {
private int id;//
private Integer parentId;
private List<NodeEntity> children;
}
``
集合list封装成树,多个子树,任意拼接,任何层级搜索
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...