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<>(nodes.size());
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);
}
}
}
/**
* 按照每頁(yè)的id,獲取相應(yīng)的樹(shù)杈
*
* @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);
//模擬獲取當(dāng)前頁(yè)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);
//假設(shè)上面的樹(shù)里面是根據(jù)上面的4條數(shù)據(jù)遞歸查詢出來(lái)的子集合封裝成的樹(shù)?,F(xiàn)在一頁(yè)數(shù)據(jù)就是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封裝成樹(shù),多個(gè)子樹(shù),任意拼接,任何層級(jí)搜索
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- import java.util.List;import java.util.stream.Collectors;...
- 1. List簡(jiǎn)介 1>:定義是:List是元素可以重復(fù)并且有序的集合(存儲(chǔ)順序和取出順序);2>:List...
- 需求 想要提取一個(gè)數(shù)組里的幾個(gè)"不同部位"(被不像要的元素隔開(kāi))的元素組!刪去不像要的元素太麻煩,如何仍用"索引的...