如何在java中将树结构转换为节点流 [英] How to convert a tree structure to a Stream of nodes in java
问题描述
我想在 Java8 节点流中转换树.
I want to convert a tree in a Java8 stream of nodes.
这是一个存储数据的节点树,可供选择:
Here is a tree of nodes storing data which can be selected:
public class SelectTree<D> {
private D data;
private boolean selected = false;
private SelectTree<D> parent;
private final List<SelectTree<D>> children = new ArrayList<>();
public SelectTree(D data, SelectTree<D> parent) {
this.data = data;
if (parent != null) {
this.parent = parent;
this.parent.getChildren().add(this);
}
}
public D getData() {
return data;
}
public void setData(D data) {
this.data = data;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public SelectTree<D> getParent() {
return parent;
}
public void setParent(SelectTree<D> parent) {
this.parent = parent;
}
public List<SelectTree<D>> getChildren() {
return children;
}
public boolean isRoot() {
return this.getParent() == null;
}
public boolean isLeaf() {
return this.getChildren() == null || this.getChildren().isEmpty();
}
}
我想获取所选数据的集合
I want to get a collection of the selected data
我想做这样的事情:
public static void main(String[] args) {
SelectTree<Integer> root = generateTree();
List<Integer> selectedData = root.stream()
.peek(node -> System.out.println(node.getData()+": "+node.isSelected()))
.filter(node-> node.isSelected())
.map(node-> node.getData())
.collect(Collectors.toList()) ;
System.out.println("
selectedData="+selectedData);
}
private static SelectTree<Integer> generateTree() {
SelectTree<Integer> n1 = new SelectTree(1, null);
SelectTree<Integer> n11 = new SelectTree(11, n1);
SelectTree<Integer> n12 = new SelectTree(12, n1);
n12.setSelected(true);
SelectTree<Integer> n111 = new SelectTree(111, n11);
n111.setSelected(true);
SelectTree<Integer> n112 = new SelectTree(112, n11);
SelectTree<Integer> n121 = new SelectTree(121, n12);
SelectTree<Integer> n122 = new SelectTree(122, n12);
return n1;
}
问题是找到 stream()
的实现,我想我可以帮助一些人分享我的解决方案,我很想知道是否有一些问题或更好的方法来做到这一点.
The problem was to find the implementation of stream()
and I think I could help some people sharing my solution and I would be interested in knowing if there are some issues or better ways of doing this.
起初是针对primefaces TreeNode
,但我将问题推广到各种树.
At first it was for primefaces TreeNode
but I generalize the problem to all kinds of trees.
推荐答案
我发现 stream()
的这个实现是一个 DFS 树遍历:
I find this implementation of stream()
which is a DFS tree traversal:
public class SelectTree<D> {
//...
public Stream<SelectTree<D>> stream() {
if (this.isLeaf()) {
return Stream.of(this);
} else {
return this.getChildren().stream()
.map(child -> child.stream())
.reduce(Stream.of(this), (s1, s2) -> Stream.concat(s1, s2));
}
}
}
如果你不能像 primefaces TreeNode
(org.primefaces.model.TreeNode
) 那样更改树的实现,你可以在其他类中定义一个方法:
If you can't change the tree implementation like for primefaces TreeNode
(org.primefaces.model.TreeNode
) you can define a method in an other class:
public Stream<TreeNode> stream(TreeNode parentNode) {
if(parentNode.isLeaf()) {
return Stream.of(parentNode);
} else {
return parentNode.getChildren().stream()
.map(childNode -> stream(childNode))
.reduce(Stream.of(parentNode), (s1, s2) -> Stream.concat(s1, s2)) ;
}
}
这篇关于如何在java中将树结构转换为节点流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!