java - Recursive through List-Elements, which can have List too -
i'm looking suitable recursive way achieve following: element can have list l1 contains further elements such a, e.g. b,c , d. these elements (b,c , d) can have list l2, l3, l4 too. need go through these lists too. background want objects lists of elements contains "lb" @ end of name (retrieved getname()). objects of lists have same type. how achieve this? don't know how many elements , lists there be, think recursive solution proper one?
basically have tree structure there, means need form of tree traversal. let's assume have tree-like structure:
class node<t>{ t value; list<node<t>> children = new arraylist<>(); }
now, if want apply callback c each of these nodes, this:
public <t> void visit(node<t> rootnode, consumer c){ c.consume(rootnode.value); rootnode.children.foreach(n -> visit(n, c)); }
this called depth-first traversal.
Comments
Post a Comment