mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-12-12 20:29:27 +08:00
✨ 优化 tree 泛型
This commit is contained in:
parent
d40e743be9
commit
a9dcbaacec
@ -23,11 +23,14 @@ import java.util.List;
|
|||||||
*
|
*
|
||||||
* @author zhuangqian
|
* @author zhuangqian
|
||||||
*/
|
*/
|
||||||
public class ForestNodeManager {
|
public class ForestNodeManager<T extends INode> {
|
||||||
|
|
||||||
private List<INode> list;// 森林的所有节点
|
/**
|
||||||
|
* 森林的所有节点
|
||||||
|
*/
|
||||||
|
private List<T> list;
|
||||||
|
|
||||||
public ForestNodeManager(List<INode> items) {
|
public ForestNodeManager(List<T> items) {
|
||||||
list = items;
|
list = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,9 +42,10 @@ public class ForestNodeManager {
|
|||||||
*/
|
*/
|
||||||
public INode getTreeNodeAT(int id) {
|
public INode getTreeNodeAT(int id) {
|
||||||
for (INode forestNode : list) {
|
for (INode forestNode : list) {
|
||||||
if (forestNode.getId() == id)
|
if (forestNode.getId() == id) {
|
||||||
return forestNode;
|
return forestNode;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,12 +54,13 @@ public class ForestNodeManager {
|
|||||||
*
|
*
|
||||||
* @return 树的根节点集合
|
* @return 树的根节点集合
|
||||||
*/
|
*/
|
||||||
public List<INode> getRoot() {
|
public List<T> getRoot() {
|
||||||
List<INode> roots = new ArrayList<>();
|
List<T> roots = new ArrayList<>();
|
||||||
for (INode forestNode : list) {
|
for (T forestNode : list) {
|
||||||
if (forestNode.getParentId() == 0)
|
if (forestNode.getParentId() == 0) {
|
||||||
roots.add(forestNode);
|
roots.add(forestNode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return roots;
|
return roots;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,9 +31,9 @@ public class ForestNodeMerger {
|
|||||||
* @param items 节点域
|
* @param items 节点域
|
||||||
* @return 多棵树的根节点集合
|
* @return 多棵树的根节点集合
|
||||||
*/
|
*/
|
||||||
public static List<INode> merge(List<INode> items) {
|
public static <T extends INode> List<T> merge(List<T> items) {
|
||||||
ForestNodeManager forestNodeManager = new ForestNodeManager(items);
|
ForestNodeManager<T> forestNodeManager = new ForestNodeManager<>(items);
|
||||||
for (INode forestNode : items) {
|
for (T forestNode : items) {
|
||||||
if (forestNode.getParentId() != 0) {
|
if (forestNode.getParentId() != 0) {
|
||||||
INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId());
|
INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId());
|
||||||
node.getChildren().add(forestNode);
|
node.getChildren().add(forestNode);
|
||||||
|
Loading…
Reference in New Issue
Block a user