i have following scenario
object has -- list<b> -- list<c> -- object
object contains: list<b>
, list<c>
i want collect objects of type 'b' in list after traversal in given object 'a'.
note: list<c>
may contain object 'a', iteration becomes recursive.
i/p: object = {list<b>,list<c>} o/p: list<b>//list of objects of type b
what best way iterate on , collect objects of type 'b' list in java 8.
thanks
what don't understand why has done soly stream? why don't create recursive function? , list<a>
not contain list<a>
, list<b>
, object a
contains 2 lists.
here simple recursive method should you, keep in mind did not provide code of , b cannot know:
public list<b> getallbs(list<a> alist) { return alist.stream() .map(a -> getallbs(a)) .collect(collectors.tolist()); } private list<b> getallbs(a somea) { list<b> returnlist = new arraylist<b>(); returnlist.addall(somea.getlistofb()); for(a othera: somea.getlistofa()) { returnlist.addall(getallbs(othera); } return returnlist; }
Comments
Post a Comment