倒排Tree树
需求说明为:
树节点存在(标识)或者叶子节点存在标识 都需要展示出来 存在※的节点及其上级节点需要返回 其余节点需要剔除
A() -------------------------------------------------------------------根节点
A1.1(), A1.2()---------------------------------------------------------一级节点
A1.1.1() ,A1.1.2() , A1.1.3(※) A1.2.1() -------------------------------------------------------二级节点
A1.1.1.1(※) A1.1.2.1() A1.1.2.2() A1.2.1.1() A1.2.1.2(※) -------------------------------------三级节点
此时如上的话 需要展示的结果为: 其余节点不需要
A --> A1.1
-->A1.1.3
-->A1.1.1
-->A1.1.1.1
-->A1.2
-->A1.2.1
-->A1.2.1.2
解决方案:
public static void main(String[] args) {
List<TreeNode> allDataCollection = // 取出所有节点
List<TreeNode> markCollection = // 带有 ※ 标识的节点
desertAspenConstruction(allDataCollection, markCollection);
List<TreeNode> resTreeNodes = createResult(allDataCollection, false);
resTreeNodes.forEach(System.out::println);
}
public static List<TreeNode> createResult(List<TreeNode> allDataCollection, boolean needSort) {
allDataCollection.forEach(treeNode -> System.out.println("createResult : " + treeNode));
List<TreeNode> resList = new ArrayList<>();
allDataCollection.forEach(treeNode -> {
if (treeNode.getParentId().equals("1") && !treeNode.getIsToKeep().equals("0")) {
resList.add(treeNode);
}
});
if (needSort) Collections.sort(resList, (o1, o2) -> o1.getSn() - o2.getSn() < 0 ? 1 : -1);
return resList;
}
/**
* 功能描述: <br> 沙漠白杨建设
* [No modification without permission, at your own risk (*)]
* 〈〉
* [allDataCollection, markCollection]
*
* @return: void
* @since: 1.0.0
*/
public static void desertAspenConstruction(List<TreeNode> allDataCollection, List<TreeNode> markCollection) {
allDataCollection.forEach(treeNode -> {
if (StringXutil.isNullOrBlank(treeNode.parentId)) treeNode.parentId = "1";
if (StringXutil.isNullOrBlank(treeNode.isToKeep)) treeNode.isToKeep = "0";
boolean flag = true;
if (markCollection == null || markCollection.size() == 0 || markCollection.isEmpty()) {
flag = false;
}
if (flag) {
markCollection.forEach(treeNode1 -> {
if (treeNode.getId().equals(treeNode1.getId())) {
treeNode.setIsToKeep("1");
}
});
} else {
treeNode.setIsToKeep("1");
}
});
allDataCollection.forEach(treeNode -> {
basicDataFormattingRecursion(allDataCollection, treeNode);
});
bulidTreeChildren(allDataCollection);
}
/**
* 功能描述: <br> 基本数据格式化递归
* [No modification without permission, at your own risk (*)]
* 〈〉
* [allList, treeNode1, flag] [ 全部数据集合 , 随机节点(会遍历所有上级节点) , flag 是否去减掉没有标志的数据() ]
*
* @return: void
* @since: 1.0.0
*/
public static void basicDataFormattingRecursion(List<TreeNode> allDataCollection, TreeNode treeNode1) {
if (treeNode1.getIsToKeep().equals("1") || treeNode1.getIsToKeep().equals("2")) {
allDataCollection.forEach(treeNode -> {
if (treeNode1.getParentId().equals(treeNode.getId())) {
if (StringXutil.isNullOrBlank(treeNode.getIsToKeep())) treeNode.isToKeep = "0";
if (treeNode.getIsToKeep().equals("0")) treeNode.setIsToKeep("2");
if (StringXutil.isNullOrBlank(treeNode.getRemark())) {
treeNode.setRemark(treeNode1.getId() + ",");
} else {
treeNode.setRemark(treeNode.getRemark() + treeNode1.getId() + ",");
}
if (!(treeNode.getParentId()).equals("1"))
basicDataFormattingRecursion(allDataCollection, treeNode);
}
});
}
}
/**
* 功能描述: <br> 建造树儿子
* [No modification without permission, at your own risk (*)]
* 〈〉
* [allDataCollection] [全部数据集合]
*
* @return: void
* @since: 1.0.0
*/
public static void bulidTreeChildren(List<TreeNode> allDataCollection) {
allDataCollection.forEach((treeNode) -> {
List<TreeNode> childrenList = new ArrayList<>();
if (!StringXutil.isNullOrBlank(treeNode.getRemark())) {
String[] arr = treeNode.getRemark().split(","); // 1,2,3,4,6,7,8
for (String temp : arr) {
allDataCollection.forEach(treeNode1 -> {
if (treeNode1.getId().equals(temp)) childrenList.add(treeNode1);
});
}
}
removeDuplicate(childrenList);
treeNode.setChildren(childrenList);
});
}
public static <T> void removeDuplicate(List<T> list) {
List<T> result = new ArrayList<T>(list.size());
for (T str : list) {
if (!result.contains(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}
@ToString
@Data
public class TreeNode {
protected String id;
protected String parentId;
protected int sn;
protected String name;
protected String remark;
protected String isToKeep;
protected List<TreeNode> children = new ArrayList<TreeNode>();
public void add(TreeNode node) {
children.add(node);
}
public String getParentId() {
return StringXutil.isNullOrBlank(parentId) ? "1" : parentId;
}
public void setParentId(String parentId) {
this.parentId = StringXutil.isNullOrBlank(parentId) ? "1" : parentId;
}
}