我想將樹轉換中的節點如何樹結構轉換中的節點的流在Java中
這裏的Java8流是存儲數據至極節點樹可以選擇
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();
}
}
我想選擇的數據 集合我想要做這樣的事情:
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("\nselectedData="+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 ;
}
問題是要找到流的實現(),我想我可以幫助一些人們分享我的解決辦法,我會劉曉丹知道,如果有一些問題或者更好的方式來這樣做
起初它是爲primefaces樹節點,但我概括這個問題所有種樹
編輯我刪除了樹的抽象類無用此puporse以簡化代碼
可能的重複[在Java中,我如何高效優雅地流式傳輸樹節點的後代?](http://stackoverflow.com/questions/32749148/in-java-how-do-i-efficiently-and-優雅流-A-樹節點後裔) – 2017-05-22 08:48:53