private List<WorkPeriodResp> getChildren(WorkPeriodResp root, List<WorkPeriodResp> all) { return all.stream().filter(m -> Objects.equals(m.getParentCode(), root.getNodeCode())).peek( (m) -> m.setChildren(getChildren(m, all)) ).collect(Collectors.toList()); }
时间: 2024-04-20 08:25:59 浏览: 18
这段代码实现了一个递归函数 `getChildren`,用于获取给定根节点下的所有子节点。
函数接受两个参数:`root` 表示根节点,`all` 表示包含所有节点的列表。
函数使用 Java 8 的 Stream API 对 `all` 列表进行过滤操作,保留满足条件的节点。条件判断是通过 `Objects.equals(m.getParentCode(), root.getNodeCode())` 实现的,即判断节点的父节点代码是否等于根节点的代码。
在过滤之后,使用 `peek` 方法对每个满足条件的节点进行操作。操作中调用了递归函数 `getChildren(m, all)`,即传入当前节点和所有节点列表,以获取当前节点的子节点列表。然后,将子节点列表设置为当前节点的子节点。
最后,使用 `collect` 方法将过滤和操作后的节点列表收集起来,并作为结果返回。
综上所述,这段代码的作用是根据给定的根节点和包含所有节点的列表,递归地获取根节点下的所有子节点,并返回一个包含所有子节点的列表。
相关问题
java 把一个 list<object>中的数据按照树结构排序
如果你要把一个 `List<Object>` 中的数据按照树结构排序,可以参考以下步骤:
1. 定义一个树结构的节点类,包含节点的值,父节点和子节点。
2. 遍历 `List<Object>` 中的所有元素,将每个元素转换为节点对象,并插入到树中。
3. 对树进行遍历,按照节点值的大小关系,将节点从树中移除并插入到正确的位置上。
4. 遍历树,将所有节点的值按照树的结构排序后,再将它们存储回 `List<Object>` 中。
以下是一个示例代码,可以帮助你实现这个功能:
```java
public class TreeNode {
private Object value;
private TreeNode parent;
private List<TreeNode> children;
public TreeNode(Object value) {
this.value = value;
this.children = new ArrayList<>();
}
public void addChild(TreeNode child) {
this.children.add(child);
child.parent = this;
}
public void removeChild(TreeNode child) {
this.children.remove(child);
child.parent = null;
}
public Object getValue() {
return value;
}
public List<TreeNode> getChildren() {
return children;
}
public boolean isLeaf() {
return children.isEmpty();
}
}
public class TreeSorter {
public static List<Object> sort(List<Object> list) {
TreeNode root = new TreeNode(null);
for (Object value : list) {
TreeNode node = new TreeNode(value);
insertNode(root, node);
}
List<Object> sortedList = new ArrayList<>();
traverseTree(root, sortedList);
return sortedList;
}
private static void insertNode(TreeNode root, TreeNode node) {
TreeNode parent = findParent(root, node);
parent.addChild(node);
}
private static TreeNode findParent(TreeNode root, TreeNode node) {
if (root.isLeaf()) {
return root;
}
for (TreeNode child : root.getChildren()) {
if (compareValues(node.getValue(), child.getValue()) < 0) {
return findParent(child, node);
}
}
return root;
}
private static void traverseTree(TreeNode node, List<Object> sortedList) {
if (node.isLeaf()) {
sortedList.add(node.getValue());
return;
}
for (TreeNode child : node.getChildren()) {
traverseTree(child, sortedList);
}
}
private static int compareValues(Object obj1, Object obj2) {
if (obj1 == null && obj2 == null) {
return 0;
} else if (obj1 == null) {
return -1;
} else if (obj2 == null) {
return 1;
} else if (obj1 instanceof Comparable && obj2 instanceof Comparable) {
return ((Comparable) obj1).compareTo(obj2);
} else {
return obj1.toString().compareTo(obj2.toString());
}
}
}
```
使用这个代码,你可以像下面这样对一个 `List<Object>` 进行排序:
```java
List<Object> list = Arrays.asList("b", "d", "a", "c", "e");
List<Object> sortedList = TreeSorter.sort(list);
System.out.println(sortedList); // 输出 [a, b, c, d, e]
```
需要注意的是,这个示例代码中比较节点值的方法 `compareValues` 会根据节点值的类型进行比较,如果节点值是 `null`,则认为它小于任何非空值。如果节点值没有实现 `Comparable` 接口,则将它们转换为字符串进行比较。如果你的节点值类型比较特殊,可以根据需要修改这个方法。
JAVA实现list<string>转换成多叉树结构代码
以下是JAVA实现list<string>转换成多叉树结构的代码示例:
```
import java.util.ArrayList;
import java.util.List;
public class TreeNode {
private String value;
private List<TreeNode> children;
public TreeNode(String value) {
this.value = value;
this.children = new ArrayList<>();
}
public String getValue() {
return value;
}
public List<TreeNode> getChildren() {
return children;
}
public void addChild(TreeNode child) {
this.children.add(child);
}
public static TreeNode buildTree(List<String> list) {
TreeNode root = new TreeNode("");
for (String s : list) {
TreeNode current = root;
String[] values = s.split(",");
for (String value : values) {
current = addChildIfNotExist(current, value);
}
}
return root.getChildren().get(0);
}
private static TreeNode addChildIfNotExist(TreeNode parent, String value) {
for (TreeNode child : parent.getChildren()) {
if (child.getValue().equals(value)) {
return child;
}
}
TreeNode newChild = new TreeNode(value);
parent.addChild(newChild);
return newChild;
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A,B,C,D");
list.add("A,B,C,E,F");
list.add("A,G,H,I");
TreeNode root = TreeNode.buildTree(list);
printTree(root, 0);
}
private static void printTree(TreeNode node, int depth) {
System.out.println(getIndent(depth) + node.getValue());
for (TreeNode child : node.getChildren()) {
printTree(child, depth + 1);
}
}
private static String getIndent(int depth) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < depth; i++) {
sb.append("\t");
}
return sb.toString();
}
}
```
使用示例:
```
List<String> list = new ArrayList<>();
list.add("A,B,C,D");
list.add("A,B,C,E,F");
list.add("A,G,H,I");
TreeNode root = TreeNode.buildTree(list);
printTree(root, 0);
```
输出结果:
```
A
B
C
D
E
F
G
H
I
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)