The question is as follows
My class
public class Node{
private String name;
private Node parent;
private List<Node> children;
public Node() {
}
public Node(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
}
I found several examples to create a tree list using stream, but what I need is to create a list of only 'parents' Example:
I have the following hierarchy:
1
1.1
1.1.1
1.1.1.1
1.1.1.2
1.1.1.3
What I need is the following:
I'm going to make a query on this tree, I'm going to start node 1.1.1.3, I want a list with himself and his parents
[1.1.1.3, 1.1.1, 1.1, 1]
I was able to do it using a global list and fill it with a recursive method, but I wanted to know if it has a more elegant way to do it. I read about streams of java 8, but I'm not sure if it catches the way I need it.
Any ideas?
Thank you!