Is it possible to fill a tree list in reverse using stream?

Asked

Viewed 40 times

1

The point is this

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 to do is 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 this:

I’m going to make a query on this tree, I pass as a parameter 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 filling it with a recursive method, but I was wondering if you had a more elegant way of doing it. I read about java 8 streams, but I’m not sure if it fits the way I need it.

Any idea?

Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.